var Pyra={
	toggleCheckbox:function(e){
		if($(e).prev().attr('checked')){
			$(e).prev().attr('checked',false);
			$(e).css('text-decoration','none').css('font-weight','normal');
		}else{
			$(e).prev().attr('checked',true);
			$(e).css('text-decoration','underline').css('font-weight','bold');
		}
	},
	sActive:false,
	slider:function(){
		$(".btn-slide").click(function(){
			$(this).blur();
			if($(this).hasClass("active")){
				$("#panel").slideUp("normal");
				$(this).removeClass("active");
			}else{
				$("#panel").slideDown("normal");
				$(this).addClass("active");
			}
		});
		$('#vmj-home-search').focus(function(){
			$("#panel").slideDown("normal");
			$(".btn-slide").addClass("active");
		});
		$('#vmj-home-search').blur(function(){
			$("#panel").slideUp("normal");
			$(".btn-slide").removeClass("active");
		});
		$('#vmj-search-c1').click(function(){
			$("#panel").slideDown("normal");
			$(".btn-slide").addClass("active");
		});
	},
	cascade:function(){
		$('.vui-cascade-header').click(function(){
			if($(this).hasClass("active")){
				$(this).removeClass("active");
				$(this).find('.vui-icon').removeClass('vui-icon-triangle-s').addClass('vui-icon-triangle-e');
				$(this).next().slideUp('fast');
			}else{
				$(this).addClass("active");
				$(this).find('.vui-icon').removeClass('vui-icon-triangle-e').addClass('vui-icon-triangle-s');
				$(this).next().slideDown('fast');
			}
		});
		$('.vmj-cascade-refresh').click(function(){
			var uri=$('#vmjurl').val();
			var qr='';
			var vmcid='';
			$('.vmcid').each(function(i){
				if(this.checked){
					vmcid+='&'+this.name+"="+this.value;
				}
			});
			qr=vmcid;
			var vmf='';
			$('.vmf').each(function(i){
				if(this.checked){
					vmf+='&'+this.name+"="+this.value;
				}
			});
			qr+=vmf;
			var vmsid='';
			$('.vmsid').each(function(i){
				if(this.checked){
					vmsid+='&'+this.name+"="+this.value;
				}
			});
			qr+=vmsid;
			qr+=$('#on_sale').attr('checked')?'&on_sale=1':'';
			qr+=$('#free_shipping').attr('checked')?'&free_shipping=1':'';
			qr+='&p_min='+$('#p_min').val();
			qr+='&p_max='+$('#p_max').val();
			if(qr!=''){qr='?'+qr;}
			location.href='http://'+document.domain+uri+qr;
		});
	},
	mi:null,
	_timer:null,
	menu:function(){
		$('#dept_menu').popupmenu({
			target: "#vmj_menu_list",
			time: 500,
			speed:"fast"
		});
		$('.cat').addClass('vmj_menu_out').mouseover(function(){
			clearTimeout(Pyra._timer);
			$('.cat').removeClass('vmj_menu_over').addClass('vmj_menu_out');
			var r=$(this);
			r.removeClass('vmj_menu_out').addClass('vmj_menu_over');
			Pyra.mi=r.get(0).id.substr(4,11);
			$('#vmj_menu_content_title').html('');
			$('#vmj_menu_content_data').html('');
			$('#vmj_menu_loader').show();
			$('#vmj_menu_content').fadeIn('normal');
			Pyra._timer = setTimeout(
			function(){
				$.post('/xml_http_req/vmj_menu_data',{c:Pyra.mi},
					function(data){
						$('#vmj_menu_loader').hide();
						if(data.documentElement.firstChild.nodeValue!='null'){
							var title=data.documentElement.childNodes[0].firstChild.nodeValue;
							var content=data.documentElement.childNodes[1].firstChild.nodeValue;
							$('#vmj_menu_content_title').html(title);
							$('#vmj_menu_content_data').html(content);
						}
					}
				);
			}
			,300);
		}).mouseout(function(){
			
		});
	},
	myaccount:function(){
		// date picker
		$('.date-picker').datepicker({showOn: 'both', buttonImage: '/images/date.gif', buttonImageOnly: true, changeMonth: true, changeYear: true, dateFormat:'yy-mm-dd'});
		$('#login-form').validate();
		$('#vcard-add-form').validate({
			rules:{
				trn:{ 
					minlength:9
				}
			}
		});
		$('#vcard-add-form img').click(function(){
			var vCardDesign = $(this).attr('src');
			$('#vCardDesign').val(vCardDesign);
			$('#vcard-add-form img').removeClass('selected');
			$(this).addClass('selected');
		});
		$('#name-setting-form').validate({
			rules:{
				security_code:{
					required: true,
					remote: '/captcha/process.php'
				}
			}
		});
		$('#emailpassword-setting-form').validate({
			rules:{
				newpassword:{ 
					minlength:6 
				},
				confirmpassword:{
					minlength: 6,
					equalTo: '#newpassword'
				},
				security_code:{
					required: true,
					remote: '/captcha/process.php'
				}
			}
		});
		$('#secretquestion-setting-form').validate({
			rules:{
				confirmanswer:{ equalTo: '#newanswer' },
				security_code:{
					required: true,
					remote: '/captcha/process.php'
				}
			}
		});
		$('#recoverpassword-setting-form').validate({
			rules:{
				newpassword:{ 
					minlength:6 
				},
				confirmpassword:{
					minlength: 6,
					equalTo: '#newpassword'
				},
				confirmemail:{
					equalTo: '#email'
				},
				security_code:{
					required: true,
					remote: '/captcha/process.php'
				}
			}
		});
		$('#refer-form').validate();
		$('.get-contact').click(function(){
			var provider = $(this).attr('rel');
			var user = $('#'+provider+'-username').val();
			var pass = $('#'+provider+'-password').val();
			$(this).attr('disabled','disabled');
			$(this).attr('value','processing...');
			$.post('/xml_http_req/refer',{u:user,p:pass,pr:provider},
				function(data){
					$('#'+provider+'-results').html(data.documentElement.firstChild.nodeValue);
					$('#'+provider+'-get-contact').removeAttr('disabled');
					$('#'+provider+'-get-contact').attr('value','Get My Contacts');
					if(data.documentElement.firstChild.nodeValue != 'error'){
						$('.send-contact').removeAttr('disabled');
					}else{
						$('#'+provider+'-results').html('Unable to get contacts !. Please try again!');
					}
				}
			);
		});
		// view notes modal box
		$('.printInvoice').click(function(){
			var objID = $(this).attr('id');
			var rowID = objID.replace(/printInvoice_/, '');
			window.open('http://mystore.shopvmj.com/xml/getOrderInvoice.php?r='+rowID,'popWin','width=700,height=500,toolbar=0,location=0,directories=0,status=0,menubar=0,dependent=1,scrollbars=1');
		});
	},
	productDetails:function(){		
		$("#tabs").tabs();
		$("#login-reg-dialog").dialog({
			bgiframe: true,
			autoOpen: false,
			width: 520,
			height: 480,
			draggable: false,
			modal: true
		});
		$('#xd-register').submit(function(){
			$.post('/xml_http_req/register',{f:$("#xd-register [name='firstName']").val(),l:$("#xd-register [name='lastName']").val(),e:$("#xd-register [name='email']").val(),p:$("#xd-register [name='passwd']").val(),pc:$("#xd-register [name='passwd_c']").val(),c:$('.validate-termsAgree').get(0).checked},
				function(data){
					if(data.documentElement.childNodes[0].firstChild.nodeValue==1){
						$("#login-reg-dialog").close();
					}else{
						$('#reg-alert').html('<div class="ui-widget"><div style="padding: 0pt 0.7em; margin-top: 20px;" class="ui-state-highlight ui-corner-all"><p><span style="float: left; margin-right: 0.3em;" class="ui-icon ui-icon-info"/><strong>Hey!</strong> '+data.documentElement.childNodes[1].firstChild.nodeValue+'</p></div></div>');
					}
				}
			);
			return false;
		});
		$('#xd-login').submit(function(){
			$.post('/xml_http_req/login',{e:$("#xd-login [name='email']").val(),p:$("#xd-login [name='passwd']").val()},
				function(data){
					if(data.documentElement.childNodes[0].firstChild.nodeValue==1){
						$('#login-reg-dialog').dialog('close');
						$('.review-write-btn').hide();
						$('#review-fm').show();
					}else{
						$('#log-alert').html('<div class="ui-widget"><div style="padding: 0pt 0.7em; margin-top: 20px;" class="ui-state-highlight ui-corner-all"><p><span style="float: left; margin-right: 0.3em;" class="ui-icon ui-icon-info"/><strong>Hey!</strong> '+data.documentElement.childNodes[1].firstChild.nodeValue+'</p></div></div>');
					}
				}
			);
			return false;
		});
		$('.showlogin').click(function(){
			$('.login').show();
			$('.register').hide();
		});
		$('.showregister').click(function(){
			$('.login').hide();
			$('.register').show();
		});
		$('.comment-remove').click(function(){
			var r=$(this);
			$.post('/xml_http_req/product_comment_remove',{c:r.get(0).id.substr(4,11)},
				function(data){
					if(data.documentElement.firstChild.nodeValue==1){
						r.parent().parent().fadeOut('fast', function(){
							$(this).addClass('ui-state-highlight').html('Your product comment has been removed.').fadeIn();
						});
					}
				}
			);
		});
		$('.vmj-prod-image-show').mouseover(function(){
			$('.vmj-prod-image').hide();
			$('#'+$(this).get(0).id+'-lg').fadeIn();
		});
		$("#preview-dialog").dialog({
			bgiframe: true,
			autoOpen: false,
			width: 500,
			height: 530,
			draggable: false,
			modal: true
		});
	},
	sortStore:function(c){
		if(c=='all'){
			$('.stores').show();
		}else if(c=='0 - 9'){
			$('.stores').hide();
			for(var i=0;i<10;i++){
				$('.store_'+i).show();
			}
		}else{
			$('.stores').hide();
			$('.store_'+c).show();
		}
	},
	productGroup:function(){
		$('.vui-prod-image').mouseover( function() {
			$(this).children('.vui-prod-zoom').show();
		}).mouseout( function() {
			$(this).children('.vui-prod-zoom').hide();
		});
		$('.vui-prod-himage').mouseover( function() {
			$(this).children('.vui-prod-zoom').show();
		}).mouseout( function() {
			$(this).children('.vui-prod-zoom').hide();
		});
		Pyra.cascade();
		$("#preview-dialog").dialog({
			bgiframe: true,
			autoOpen: false,
			width: 500,
			height: 530,
			draggable: false,
			modal: true
		});
		$('.vui-cascade :checkbox').click(function(){
			if($(this).attr('checked')){
				$(this).next().css('text-decoration','underline').css('font-weight','bold');
			}else{
				$(this).next().css('text-decoration','none').css('font-weight','normal');
			}
		});
	},
	search:function(){
		$('.vui-prod-image').mouseover( function() {
			$(this).children('.vui-prod-zoom').show();
		}).mouseout( function() {
			$(this).children('.vui-prod-zoom').hide();
		});
		$('.vui-prod-himage').mouseover( function() {
			$(this).children('.vui-prod-zoom').show();
		}).mouseout( function() {
			$(this).children('.vui-prod-zoom').hide();
		});
		$("#preview-dialog").dialog({
			bgiframe: true,
			autoOpen: false,
			width: 480,
			height: 480,
			draggable: false,
			modal: true
		});
	},
	previewImg:function(f){
		$('#preview-dialog').html('<div style="text-align:center;"><img src="'+f+'" /></div>');
		$('#preview-dialog').dialog('open');
	},
	reqPrice:function(p){
		$.post('/xml_http_req/fm_login_reg/'+p,"",
			function(data){
				$('#preview-dialog').html(data.documentElement.firstChild.nodeValue);
				$('#preview-dialog').dialog('option', 'title', 'Join ShopVMJ.com to send a price request.');
				$("#preview-dialog").dialog('open');
				$('#x-register').submit(function(){
					$.post('/xml_http_req/register',{f:$("#x-register [name='firstName']").val(),l:$("#x-register [name='lastName']").val(),e:$("#x-register [name='email']").val(),p:$("#x-register [name='passwd']").val(),pc:$("#x-register [name='passwd_c']").val(),c:$('.validate-termsAgree').get(0).checked},
						function(data){
							if(data.documentElement.childNodes[0].firstChild.nodeValue==1){
								$.post('/xml_http_req/fm_price_request/'+p,"",
									function(data){
										$("#preview-dialog").html(data.documentElement.firstChild.nodeValue);
									}
								);
							}else{
								$('.reg-alert').html('<div class="ui-widget"><div style="padding: 0pt 0.7em; margin-top: 20px;" class="ui-state-highlight ui-corner-all"><p><span style="float: left; margin-right: 0.3em;" class="ui-icon ui-icon-info"/><strong>Hey!</strong> '+data.documentElement.childNodes[1].firstChild.nodeValue+'</p></div></div>');
							}
						}
					);
					return false;
				});
				$('#x-login').submit(function(){
					$.post('/xml_http_req/login',{e:$("#x-login [name='email']").val(),p:$("#x-login [name='passwd']").val()},
						function(data){
							if(data.documentElement.childNodes[0].firstChild.nodeValue==1){
								$.post('/xml_http_req/fm_price_request/'+p,"",
									function(data){
										$("#preview-dialog").html(data.documentElement.firstChild.nodeValue);
									}
								);
							}else{
								$('.log-alert').html('<div class="ui-widget"><div style="padding: 0pt 0.7em; margin-top: 20px;" class="ui-state-highlight ui-corner-all"><p><span style="float: left; margin-right: 0.3em;" class="ui-icon ui-icon-info"/><strong>Hey!</strong> '+data.documentElement.childNodes[1].firstChild.nodeValue+'</p></div></div>');
							}
						}
					);
					return false;
				});
			}
		);
	},
	sendPriceReq:function(p){
		$.post('/xml_http_req/req_price/'+p,"",
			function(data){
				if(data.documentElement.firstChild.nodeValue==1){
					alert('Your price request has been sent to the vendor.');
				}else{
					alert('An error occured while trying to process your price request. Please try again.');
				}
				$("#preview-dialog").dialog('close');
			}
		);
	},
	deselectAll:function(fm,objName){
		$(fm).find('input[name="'+objName+'"]').removeAttr('checked');
	},
	selectAll:function(fm,objName){
		$(fm).find('input[name="'+objName+'"]').attr('checked', 'checked');
	}
};