// JavaScript Document
var headerMenu={
	flString:'',
	left:'74px',
	top:'30px',
	bgColor:'#6495AB',
	preBgColor:'',
	showCategories:function(thisA){
		if($('#floatCategoryDiv').css('display')=='block'){
			//this.removeCategories();
		}else{
			$('#flSelectHead').css('visibility','hidden');
			$('#floatCategoryDiv').css('position','absolute');
			$('#floatCategoryDiv').css('left',this.left);
			$('#floatCategoryDiv').css('top',this.top);
			$('#floatCategoryDiv').show('fast');
			$('.topics,#centerBody').mouseover(
				function(){
					$('#floatCategoryDiv').hide('fast',function(){headerMenu.removeCategories(thisA);});
				}							
			);
		}
	},
	removeCategories:function(thisA){
		$('#flSelectHead').css('visibility','visible');
		$('#floatCategoryDiv').hide('fast');
	}
}

var subjection={
	preKeyWords:'',
	left:'233px',
	top:'71px',
	width:'345px',
	bgColor:'#ffffff',
	padding:'5px',
	data:'',
	oEvent:new Object(),
	setSubjection:function(e){
		var oEvent = e||window.event; 
		keyCode=oEvent.keyCode;
		var thisInput=$('.inputSearch');
		if(keyCode!=40&&keyCode!=38){
			this.removeSubjectionDiv();
		}
		var keyWords=trim(thisInput.val());
		if(keyWords!=''&&this.preKeyWords!=keyWords){
			this.preKeyWords=keyWords;
			this.getSubjection(keyWords,oEvent);
		}else if(keyWords==''){
			this.preKeyWords=keyWords;
		}
	},
	setDiv:function(){
		$('#subjectionRelative').append('<div id="subjectionDiv"></div>');
		$('#subjectionDiv').css('display','none');
		$('#subjectionDiv').css('position','absolute');
		$('#subjectionDiv').css('left',this.left);
		$('#subjectionDiv').css('top',this.top);
		$('#subjectionDiv').css('backgroundColor',this.bgColor);
		$('#subjectionDiv').css('width',this.width);
		//$('#subjectionDiv').css('padding',this.padding);
		$('#subjectionDiv').css('border','1px #cccccc solid');
		$('#subjectionDiv').html(this.data);
		$('#subjectionDiv').slideDown('fast');
		$('#centerBody,.popSearch').mouseover(
			function(){
				$('#subjectionDiv').slideUp('fast',function(){subjection.removeSubjectionDiv();});
			}							
		);
	},
	getSubjection:function(keyWords,e){
		var e = e||window.event;
		var mycallback="subjection.getSubjectionData";
		$.getJSON(JAVA_URL+"/search_keywords.jsp?callback=?",{keyword:keyWords,mycallback:mycallback});
	},
	getSubjectionData:function(json,e){
		var e = e||window.event;
		if(json.hasresult==1){
			var data="";
			for(var i=0;i<json.result.length;i++){
				data+='<div class="subjectionItem"><span class="floatRight">'+json.result[i].number+' items</span><span class="subjectionSpan">'+json.result[i].keyword+'</span></div>';	
			}
			subjection.data=data;
			subjection.setDiv();
			$('.inputSearch').unbind("keydown");
			$('.inputSearch').keydown(
				function(e){
					var e = e||window.event;
					subjection.upDownSelect(e);
				}
			);
			$('.subjectionItem').mouseover(
				function(){
					subjection.setSelected(this);
				}				  
			);
			$('.subjectionItem').click(
				function(){
					subjection.addKeywordToInput();	
				}					   
			);
		}
	},
	removeSubjectionDiv:function(){
		$('#subjectionDiv').remove();
		//$('#subjectionDiv').slideUp('fast',function(){$('#subjectionDiv').remove();});
	},
	upDownSelect:function(e){
		var oEvent = e||window.event;
		//alert(oEvent.keyCode);
		if(oEvent.keyCode==40){
			if($('.subjectionItem_change').size()==0){
				subjection.setSelected($('.subjectionItem').get(0));
			}else if($('.subjectionItem_change').next().size()==0){
				subjection.setSelected($('#subjectionDiv div:first'));
			}else{
				subjection.setSelected($('.subjectionItem_change').next().get(0));
			}
		}else if(oEvent.keyCode==38){
			if($('.subjectionItem_change').size()==0){
				subjection.setSelected($('.subjectionItem').get($('.subjectionItem').size()-1));
			}else if($('.subjectionItem_change').prev().size()==0){
				subjection.setSelected($('#subjectionDiv div:last'));
			}else{
				subjection.setSelected($('.subjectionItem_change').prev().get(0));
			}
		}else if(oEvent.keyCode==13){
			subjection.addKeywordToInput();
		}else{
			//alert(oEvent.keyCode);	
		}
	},
	setSelected:function(thisDiv){
		subjection.noSelected();
		$(thisDiv).attr('className','subjectionItem_change');
		$(thisDiv).find('span').css('color','#ffffff');
	},
	noSelected:function(){
		$('#subjectionDiv div').attr('className','subjectionItem');
		$('#subjectionDiv div').find('span').css('color','#000000');
	},
	addKeywordToInput:function(){
		this.preKeyWords=$('.subjectionItem_change').find('.subjectionSpan').html();
		if(this.preKeyWords){
			$('.inputSearch').val(this.preKeyWords);
			this.removeSubjectionDiv();
		}
	}
}

var popSearch={
	keyword:'',
	getPopSearch:function(){
		var mycallback="popSearch.getData";
		$.getJSON(JAVA_URL+"/search_keywords.jsp?callback=?",{keyword:this.keyword,mycallback:mycallback});
	},
	getData:function(json){
		if(json.hasresult==1){
			var data="";
			for(var i=0;i<json.result.length;i++){
				data+='<li class="MPLI"><a href="buyer-search-searchitem?keyword='+encodeURI(json.result[i].keyword)+'&flid=0&withWords=1">'+json.result[i].keyword+'</a></li>';	
			}
			$('#popSearchUl').html(data);
		}
	}
};

var myFavorite={
	checkUser:function(){
		var member_type=getCookie(COOKIE_PRE+"member_type");
		if(member_type==''){
			if(confirm("If you are a buyer, please login first!")){
				window.location.href=$('.login_action').attr('href');	
			}
			return false;
		}else if(member_type!='buyer'){
			alert('Your are not a buyer!');
			return false;
		}
		return true;
	},
	addItemFavor:function(itemid){
		if(this.checkUser()){
			showMsg('<img src="html/images/loading.gif" />');
			$.post(ROOT_PATH+"/adminb-favor-doadditem",{itemid:itemid},function(data) {
				if(trim(data)=='success'){
					alterMsg('Success!');	
				}else{
					alterMsg('You have added this item to your favorite products!');		
				}
			});	
		}
	},
	addCateFavor:function(flid){
		if(this.checkUser()){
			showMsg('<img src="html/images/loading.gif" />');
			$.post(ROOT_PATH+"/adminb-favor-doaddcate",{flid:flid},function(data) {
				if(trim(data)=='success'){
					alterMsg('Success!');	
				}else{
					alterMsg('You have added this Catalog to your favorite Catalogs!');		
				}
			});
		}
	},
	addMemberFavor:function(favor_uid){
		if(this.checkUser()){
			showMsg('<img src="html/images/loading.gif" />');
			$.post(ROOT_PATH+"/adminb-favor-doaddmember",{favor_uid:favor_uid},function(data) {
				if(trim(data)=='success'){
					alterMsg('Success!');	
				}else{
					alterMsg('You have added this user to your favorite sellers!');		
				}
			});
		}
	}
}

var ajaxBuy={
	itemid:0,
	showPattern:function(thisA,itemid){
		this.itemid=itemid;
		ajaxBuy.shut();	
		$(thisA).parent().append('<div id="ajaxBuyPattern" style="position:absolute"><img src="html/images/loading.gif" /></div>');
		$('#ajaxBuyPattern').css('left','10px');
		if(browser.name=='ie'){
			$('#ajaxBuyPattern').css('top','14px');
		}else{
			$('#ajaxBuyPattern').css('bottom','0px');
		}
		$('#ajaxBuyPattern').css('padding','5px');
		$('#ajaxBuyPattern').css('width','100px');
		$('#ajaxBuyPattern').css('lineHeight','20px');
		$('select').css('visibility','hidden');
		$.post(ROOT_PATH+"/buyer-buy-getpattern",{itemid:itemid},function(data) {
			$('#ajaxBuyPattern').html(data);
		});
	},
	addToCart:function(thisA,pattern_name){
		$.post(ROOT_PATH+"/buyer-buy-addtocartajax",{itemid:this.itemid,pattern_name:pattern_name},function(data) {
			$(thisA).parent().parent().parent().html('Has added!');
			//$('#shoppingCounts').html(parseInt($('#shoppingCounts').html())+1);
		});	
	},
	shut:function(){
		$('select').css('visibility','visible');
		$('#ajaxBuyPattern').remove();
	}
}

var ajaxCalculate={
	volume:0,
	weight:0,
	from_station_id:'',
	show:function(weight,volume,from_station_id){
		this.remove();
		this.weight=weight;
		this.volume=volume;
		this.from_station_id=from_station_id;
		$('#calculateDiv').append("<div id='calculateFloatDiv'>ss</div>");
		$('#calculateFloatDiv').css('position','absolute');
		$('#calculateFloatDiv').css('left','0px');
		$('#calculateFloatDiv').css('top','15px');
		$('#calculateFloatDiv').css('border','1px solid #cccccc');
		$('#calculateFloatDiv').css('padding','10px');
		$('#calculateFloatDiv').css('backgroundColor','#F7F7F7');
		$('#calculateFloatDiv').html('<img src="html/images/loading.gif" />');
		this.getLocation();
	},
	getLocation:function(){
		$.post(ROOT_PATH+"/buyer-cate-getlocation",{},function(data) {
			$('#calculateFloatDiv').html(data);
		});		
	},
	calculate:function(){
		if($('select[name="to_station_id_ajax"]').val()=='0'){
			alert('Please select where do you want to ship to!');	
			return;
		}
		var url='';
		url="from_station_id="+this.from_station_id;
		url+="&to_station_id="+$('select[name="to_station_id_ajax"]').val();
		url+="&weight="+this.weight;
		url+="&volume="+this.volume;
		url+="&currency=usd";
		url+="&type=0";
		window.open("buyer-express-dosearch?"+url,'express'); 
		this.remove();
	},
	remove:function(){
		$('#calculateFloatDiv').remove();	
	}
}

var ajaxComment={
	error:'',name:'',subject:'',content:'',verification:'',
	check:function(){
		this.error='';
		this.name=$('input[name="name"]').val();
		if(trim(this.name)==''){
			this.error+="Please input your name!\n";
		}
		
		this.subject=$('input[name="subject"]').val();
		if(trim(this.subject)==''){
			this.error+="Please input your subject!\n";
		}
		
		this.content=$('textarea[name="content"]').val();
		if(trim(this.content)==''){
			this.error+="Please input your message!\n";
		}
		
		this.verification=$('input[name="verification"]').val();
		if(trim(this.verification)==''){
			this.error+="Please input your verification code!\n";
		}
	},
	doadd:function(itemid){
		showMsg('<img src="html/images/loading.gif" />');
		$.post(ROOT_PATH+"/buyer-comment-doadd",{name:this.name,subject:this.subject,content:this.content,verification:this.verification,itemid:itemid},function(data) {																																		   
			if(data=='error'){
				alterMsg('Unknow error!');
				ajaxComment.refreshImg();
			}else if(data=='verification'){
				alterMsg('Verification code error!');
				ajaxComment.refreshImg();
			}else if(data=='success'){
				alterMsg('Success!');
				ajaxComment.refreshImg();
				ajaxComment.show('1',itemid);
				ajaxComment.emptyInput();
			}
		});		
	},
	show:function(page,itemid){
		$('#allCommentDiv').html('<img src="html/images/loading.gif" />');
		$.post(ROOT_PATH+"/buyer-comment-show",{page:page,itemid:itemid},function(data) {																																		   			$('#allCommentDiv').html(data);
		});		
	},
	refreshImg:function(){
		$('#verificationImg').attr('src',ROOT_PATH+'/buyer-comment-confirmcode?'+Math.random());	
	},
	emptyInput:function(){
		$('input[name="subject"]').val('');
		$('textarea[name="content"]').val('');
		$('input[name="verification"]').val('');
	},
	commit:function(uid){
		this.check();
		if(this.error==''){
			this.doadd(uid);
		}else{
			alert(this.error);	
		}
	}
};
