// JavaScript Document
var cp = new cpaint();    
cp.set_transfer_mode('post');
cp.set_response_type('text');
cp.set_persistent_connection(true);
cp.set_async(1);
//cp.set_proxy_url('');
cp.set_debug(0);
var archiveClicked=false;
var __frmretrievePwd="";
var __isArchived=false;






function parseScript(str){
var re1=new RegExp("\<script[a-z\"\/\= ]*\>","i"), re2=new RegExp("\<\/script>","i");
var startScript=str.split(re1);
var endScript=-1, scriptStr="";
for(j=0;j<startScript.length;j++){
endScript=startScript[j].search(re2);
scriptStr=startScript[j].substr(0,endScript).trim();
if (scriptStr!="") eval(scriptStr);
}
}


function loadData(){
	var __phpFile='index.php?controller=Ajax';	
	//
	this.reloadImg = function(obj){
		var imglist = new Array ("");		
		var imgs = new Array();		
		var count;
		if (document.images){	
			imglist=getElementsByClassName(obj,'img','');
			imglist=(imglist.length==0)?getElementsByClassName(obj,'img','img'):imglist;
		 	for (count=0; count<imglist.length; count++){
				imgs[count]=new Image(); 
				imgs[count].src=imglist[count];
			}
		}
	}
	this.maskIn = function(){
        var maskObj = $("containerMask");
        if (maskObj != null && maskObj.style) {
            maskObj.style.visibility = "hidden";
            maskObj.style.display = "none"; 
        }
    }
    this.maskOut = function(){
        var maskObj = $("containerMask");
        if (maskObj != null && maskObj.style) {
            maskObj.style.height = document.body.offsetHeight+"px";
            maskObj.style.visibility = "visible";
            maskObj.style.display = "block"; 
        }
    }
	//fade in effect
	this.fadein = function(eleFade){
		//fade in
		eleFade.style.display = "block";
		eleFade.setStyle("opacity", 0);
		eleFade.setStyle("mozOpacity", 0);	
		var myEffects = new Fx.Styles(eleFade, {duration: 200, transition: Fx.Transitions.quadIn});
		myEffects.start({
			"opacity": 1,
			"mozOpacity": 1
		}); 	
        this.maskIn();	
	}
	//fade out effect
	this.fadeout = function(eleFade){
		//fade out		
		var myEffects = new Fx.Styles(eleFade, {duration: 200, transition: Fx.Transitions.quadIn});
		myEffects.start({
			"opacity": 0,
			"mozOpacity": 0
		});	
		this.maskOut();
		$('main').focus();
	}
	
	//for fade object
	//sort content by date
	this.sortContent = function(sectionid,obj){
		if(obj.value=="oo")return;
		//fade out		
		this.fadeout($('contWrapper'));
		//
		var objTheme=$("optTheme");

		var val=objTheme.options[objTheme.selectedIndex].value;

		if(archiveClicked){
			cp.call(__phpFile,"Archive",__resLoad,sectionid,(val=='oo')?'':val,obj.value);	
		}else
			cp.call(__phpFile,"content",__resLoad,sectionid,(val=='oo')?'':val,obj.value);	
	}
	
	//load onject content of theme
	this.loadThemeContent =function(sectionid,obj){	
		//archiveClicked=false;
		if(archiveClicked==false){
			if($('btnBackSection')){
				$('btnBackSection').style.display="none";
				$('btnArchive').style.display="inline";
			}
		}
		//
		
		var sort_val=$('optDate').options[$('optDate').selectedIndex].value;
		var sorting=(sort_val=="oo")?"":sort_val;
		
		var val=$("optTheme").options[$('optTheme').selectedIndex].value;
		//fade out		
		this.fadeout($('contWrapper'));
		if(archiveClicked==false){		
			cp.call(__phpFile,"content",__resLoad,sectionid,val,sorting);		
		}else{			
			cp.call(__phpFile,"Archive",__resLoad,sectionid,val,sorting);
		}
	}
	
    //load edito archive
	this.loadArchive=function(sectionid){
		archiveClicked=true;
		if($('btnBackSection')){
			$('btnBackSection').style.display="inline";
			$('btnArchive').style.display="none";
		}
		//reset form

		var sort_val=$('optDate').options[$('optDate').selectedIndex].value;
		var sorting=(sort_val=="oo")?"":sort_val;
		
		var val=$("optTheme").options[$('optTheme').selectedIndex].value;
		//fade out		
		this.fadeout($('contWrapper'));
		
		cp.call(__phpFile,"Archive",__resLoad,sectionid,val,sorting);
		
	}
	function __resLoad(result){
		
		//return content
		$("contWrapper").setHTML(result); 
		//reload images
		objAjax.reloadImg($("contWrapper"));
		//fade in
		objAjax.fadein($('contWrapper'));
		
	}
	//end edito archive
	
    //load event list
	this.loadMonthlyEvent=function (themeid,a_Obj){
		if(themeid=="")return;
		//fade out		
		this.fadeout($('contWrapper'));
		
		//current
		var objEle = getElementsByClassName($('extra'),'a','currItem');
		if(objEle[0] && objEle[0]!="undefined")	removeClass(objEle[0],'currItem');
		addClass(a_Obj,'currItem');
		cp.call(__phpFile,"monthlyEvent",__resLoadMonth,themeid);	
	}

	function __resLoadMonth(result){   	
		$("contWrapper").setHTML(result);
		//reload images
		objAjax.reloadImg($("contWrapper"));
		//fade in
		objAjax.fadein($('contWrapper'));
	} 
	//end load event
	
	//add document to cart
	this.addCart = function(docid,eleQuan){
		var ele_val = $(eleQuan);
		var quan = ele_val.options[ele_val.selectedIndex].value;
		var numLeft = ele_val.options.length-ele_val.selectedIndex-1;
		for (var i = ele_val.options.length; i>=numLeft; i--) {
			ele_val.options[i] = null;
		}
		cp.call(__phpFile,"addCart",__resAddCart,docid,quan);
		
		
		//
	}
	function __resAddCart(result){
			
		if(result!='0')
		{
			$('label_quantity').setHTML(result);
			popUp('popAlertAddToCart');
			cp.call(__phpFile,"basketContent",function(result){$('popCartResume_items').setHTML(result);});
		}
		else{
			popUp('popAlertAddToCart_outofquota');
		}
		
		setTimeout(closeAllPopups, 3000);
	}
	
	
	//checkOut
	this.checkOut = function(){
		
		var txtFirstName=$('txtFirstName').value;
		
		var txtLastName=$('txtLastName').value;
		var txtCompany=$('txtCompany').value;
		var txtEmail=$('txtEmail').value;
		var txtAddress=$('txtAddress').value;
		var txtZip=$('txtZip').value;
		var txtCity=$('txtCity').value;
		var txtState=$('txtState').value;
		var txtCountry=$('txtCountry').value;
		
		
		
		cp.call(__phpFile,"checkOut",__resCheckOut,txtEmail,txtFirstName,txtLastName,txtCompany,txtAddress,txtZip,txtCity,txtState,txtCountry);
		
		
		//
	}
	function __resCheckOut(result){
			
		
			$('label_quantity').setHTML('0');
			$('popCheckOut').setHTML(result);
			hidePopUp('popCartResume');
			popUp('popCheckOut');
			cp.call(__phpFile,"basketContent",function(result){$('popCartResume_items').setHTML(result);});
		
		
		
	}
	
	
	function closeAllPopups(){
		hidePopUp('popAlertAddToCart_outofquota');
		hidePopUp('popAlertAddToCart');
	}
	//end add cart
	
	//function delete basket item
	this.deleteItem = function (docid,ele){		
		cp.call(__phpFile,"delete",function(result){$('label_quantity').setHTML(result);},docid);	
		
		//remove element
		var objEle = getElementsByClassName($('popCartResume'),'p','item');

		if (objEle.length > 1) {
			var el = $(objEle[ele]);
			el.remove();
			
		} else {
			var el = $(objEle[0]);
			el.remove();
		}		
		
	}
	
	//update basket quantity
	
	this.updateQuantity = function(obj, docid){
		if ($(obj.id+"_max")) {
			if ($(obj).value.toInt()>$(obj.id+"_max").value.toInt()) {
				$(obj).value = $(obj.id+"_max").value.toInt();
			}
		}
		cp.call(__phpFile,"updateBasket",
			function(result){
				
				
				$('label_quantity').setHTML(result);
				cp.call(__phpFile,"basketContent",function(result){$('popCartResume_items').setHTML(result);});
			}
			,docid,obj.value);
	}
	//function load 
	this.loadFaqs = function (faqid,a_Obj){
		if(faqid=="")return;
		//fade out		
		this.fadeout($('contWrapper'));
		
		//current
		var objEle = getElementsByClassName($('extra'),'a','currItem');
		removeClass(objEle[0],'currItem');
		addClass(a_Obj,'currItem');
		cp.call(__phpFile,"faqs",
			function(result){				
				objAjax.fadein($('contWrapper'));
				$("contWrapper").setHTML(result);
				//reload images
				objAjax.reloadImg($("contWrapper"));
				//slide show
				initSlide();
				//$$("ul.toolbox")[0].setHTML("<li>asd ad</li>");
			}
			,faqid);		
	}
	this.gotoPage = function(page){
		objAjax.loadPhoto(null,page);
	}
	//photolibrary
	this.loadPhoto=function (obj,page){
		if(obj && obj.value=="oo")return ;
		var sorting=$("optDate").options[$("optDate").selectedIndex].value;
		var optTheme=$("optTheme").options[$("optTheme").selectedIndex].value;
		var optDestination=$("optDestination").options[$("optDestination").selectedIndex].value;
		objAjax.fadein($('contWrapper'));
		$("contWrapper").setHTML("<div align='center'><img src='images/loader.gif' alt='Loading...' /></div>");
		//call ajax
		cp.call(__phpFile,"photo",
			function(result){
				objAjax.fadeout($('contWrapper'));	
				$("contWrapper").setHTML(result);
				//reload images
				objAjax.reloadImg($("contWrapper"));
				//fade in
				objAjax.fadein($('contWrapper'));
				//return content
			}
			,optTheme,optDestination,'',sorting,page);
	}
	//end photo	
	
	//load trade calendar
	this.trade=function(date,month,year){

		this.fadeout($('contWrapper'));	
		//call ajax
		cp.call(__phpFile,"trade",
			function(result){
				var arrResult = result.split('__sep__');
				//return content
				$("contWrapper").setHTML(arrResult[0]);
				$('specialMonth').setHTML(arrResult[1]);
				//reload images
				objAjax.reloadImg($("contWrapper"));
				//fade in
				objAjax.fadein($('contWrapper'));
				
			}
			,date,month,year);
		
	}
	
	//foget pass
	this.retrievePass=function (email){
		this.fadeout($('retrievePwd'));
		if(__frmretrievePwd=="")__frmretrievePwd=$("retrievePwd").innerHTML;
		cp.call(__phpFile,"getpass",
			function(result){
				objAjax.fadein($('retrievePwd'));$("retrievePwd").setHTML(result);
			}
			,email);	
	}	
	
	//load doc
	this.docItem=function(item_id,doc_id){
		this.maskOut();
		cp.call(__phpFile,"doc",
			function(result){
				//return content
				$("popBlk01").setHTML(result);
				//reload images
				objAjax.reloadImg($("popBlk01"));
				
				var type = $("popIllus01_doc_type");
				var url  = $("popIllus01_doc_url");

				if(type){
					switch(type.innerHTML){
						case "sound":
							var so = new SWFObject("audio.swf?"+String(url.innerHTML), "video", "300", "200", "8", "#dad3b3");
							so.write("popIllus01");
						break;
						case "video":
							var so = new SWFObject("video.swf?"+String(url.innerHTML), "video", "450", "350", "8", "#dad3b3");
							so.write("popIllus01");
						break;
					}
				}
				//show content
				popUp('popBlk01');
                objAjax.maskIn();
			}
			,item_id,doc_id);
	}
	//load doc
	this.mapItem=function(culture_id, city_id){
		$("popBlk01").setHTML("");
		this.maskOut();
		var loader = new Element("div").injectInside($("popBlk01"));
		loader.setStyles({'position':'absolute','background':'url(images/ajax_loader.gif) no-repeat','width':'35px','height':'35px','left':'350px', 'top':'10px'});
		popUp('popBlk01');
		cp.call(__phpFile,"destination_popup",
			function(result){
				
				//return content
				$("popBlk01").setHTML((result));
				//reload images
				parseScript(result);
				//show content
				
                objAjax.maskIn();
                
			}
			,culture_id,city_id);
	}
	//load photolibrary item photoItem
	this.photoItem=function(photo_id,text_id){
		cp.call(__phpFile,"photoItem",
			function(result){
				//return content
				$("popBlk01").setHTML(result);
				//show content
				$('photo_text').setHTML($('photo_text'+text_id).innerHTML);
				objAjax.reloadImg($("popBlk01"));
				popUp('popBlk01');				
			}
			,photo_id);
	}
}
var objAjax=new loadData(); 