var pyra={
	v3menu:function(){
		var st='Type your search here.';
		if($('#vmj-sinp input').val()==''){
			$('#vmj-sinp input').addClass('vmj-curly').val(st);
		}
		$('#vmj-sinp input').focus(function(){
			if($(this).val()==st){
				$(this).val('');
			}
			$(this).removeClass('vmj-curly');
		}).blur(function(){
			if($(this).val()==''){
				$(this).addClass('vmj-curly').val(st);
			}
		});
		var dd=$('#vmj-cat');
		$('.vmj-sopbtn').click(function(){
			$('#vmj-smenu').fadeOut();
			if(dd.css("display") == "none"){
				dd.fadeIn();
			}else{
				dd.fadeOut();
			}
		});
		$('.vmj_menu').click(function(e){
			e.stopPropagation();
		});
		$('.cid').click(function(){
			var pre=$('#vmj-soptxt span').html();
			var df='- All Categories -';
			var txt='';
			if($(".cid:checked").length>0){
				if($(".cid:checked").length==1&&pre==df){
					txt=$(this).next().html();
				}else{
					txt=$(this).next().html()+', '+pre;
				}
				if($(this).attr('checked')){
					$('#vmj-soptxt span').html(txt);
				}else{
					$('#vmj-soptxt span').html($('#vmj-soptxt span').html().replace($(this).next().html()+', ',''));
					$('#vmj-soptxt span').html($('#vmj-soptxt span').html().replace($(this).next().html(),''));
				}
			}else{
				$('#vmj-soptxt span').html(df);
			}
		});
		$('#vmj-srcfm').submit(function(){
			if($('#vmj-sbox').val()==''||$('#vmj-sbox').val()==st){
				alert('Type a search phrase first. e.g. 360 For Men By Perry Ellis');
				return false;
			}else{
				return true;
			}
		});
		$('#vmj-list li a').mouseover(function(e){
			
		});
		$('#vmj-menu').click(function(e){
			e.stopPropagation();
		});
		$(document).click(function(){
			dd.fadeOut();
			$('#vmj-smenu').fadeOut('normal',function(){$(this).css('width','1px');});
		});
		$("#vmj-list li a").hoverIntent({
			sensitivity:3,
			interval:200,
			over:pyra.expandMenu,
			timeout:300,
			out:pyra.bf
		});
	},
	expandMenu:function(){
		$('#vmj-cat').fadeOut();
		var id=$(this).attr('id').substr(2,11);
		$('#vmj-smenu .vmj-smenu-inner').html('<img src="/images/loading.gif" />');
		$('#vmj-smenu').animate({width:250},50,'linear',function(){
			$.get('/xml_http_req/vmj_menu_v3',{c:id},
				function(data){
					$('#vmj-smenu .vmj-smenu-inner').html(data.documentElement.firstChild.nodeValue);
				}
			);
		});
	},
	bf:function(){},
	getDeals:function(){
		$.get('/xml_http_req/latest_deals',{w:$('.vmj-center').width()},
			function(data){
				$('#vmj-deals').html(data.documentElement.firstChild.nodeValue);
			}
		);
	},
	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;
		});
		$('.vui-cascade :checkbox').click(function(){
			if($(this).attr('checked')){
				$(this).next().css('text-decoration','underline');
			}else{
				$(this).next().css('text-decoration','none');
			}
			$('.vui-cascade-hi').fadeIn();
		});
		$('.price-range').blur(function(){
			if($('#p_min').val()!=''&&$('#p_max').val()!=''){
				$('.vui-cascade-hi').fadeIn();
			}
		});
	},
	toggleCheckbox:function(e){
		if($(e).parent().prev().children('input').attr('checked')){
			$(e).parent().prev().children('input').attr('checked',false);
			$(e).css('text-decoration','none');
		}else{
			$(e).parent().prev().children('input').attr('checked',true);
			$(e).css('text-decoration','underline');
		}
		$('.vui-cascade-hi').fadeIn();
	},
	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();
		});
		$("#preview-dialog").dialog({
			bgiframe: true,
			autoOpen: false,
			width: 500,
			height: 530,
			draggable: false,
			modal: true
		});
	},
	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();
		}
	},
	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;
				});
			}
		);
	},
	myaccount:function(){
		$('.date-picker').datepicker({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!');
					}
				}
			);
		});
		$('.printInvoice').click(function(){
			var objID = $(this).attr('id');
			var rowID = objID.replace(/printInvoice_/, '');
			window.open('/myaccount/orderInvoice/'+rowID,'popWin','width=700,height=500,toolbar=0,location=0,directories=0,status=0,menubar=0,dependent=1,scrollbars=1');
		});
		$('.cancelOrder').click(function(){
			var objID = $(this).attr('id');
			var rowID = objID.replace(/cancelOrder_/, '');
			location.href='/myaccount/orders/cancel/'+rowID;
		});
	},
	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
		});
	}
};
$(document).ready(function(){pyra.v3menu();});
