var toplinkwish;
var toplinkcart;
var minicart;
var miniwish;
var compare;
var elem;
//var 
function checkE(){
	//toplinkwish=($$('.top-link-wishlist')!=undefined)?'.top-link-wishlist':'';
	//alert('1');
	//toplinkwish=getE('.top-link-wishlist','');
	toplinkwish=($$('.top-link-wishlist')!='')?'.top-link-wishlist':'';
	
//	if(!toplinkwish){//alert("next");
	//	toplinkwish=getToplinkwish();
	//}
	//alert(toplinkwish.href);
	//toplinkcart=($$('.top-link-cart')!=undefined)?'.top-link-cart':'';
	//toplinkcart=getE('.top-link-cart','');
	toplinkcart=($$('.top-link-cart')!='')?'.top-link-cart':'';
	//minicart=($$('.mini-cart')!='')?"yes":"no";
	//minicart=($$('.mini-cart')!='')?$$('.mini-cart')[0]:$$('.block-cart')[0];
	minicart=($$('.mini-cart')!='')?'.mini-cart':'.block-cart';
	//minicart=$$('.mini-cart');
	//minicart=getE('.mini-cart','.block-cart');
	//miniwish=($$('.mini-wishlist')!='')?$$('.mini-wishlist')[0]:$$('.block-wishlist')[0];
	miniwish=($$('.mini-wishlist')!='')?'.mini-wishlist':'.block-wishlist';
	//miniwish=getE('.mini-wishlist','.block-wishlist');
	//compare=($$('.mini-compare-products')!='')?$$('.mini-compare-products')[0]:$$('.block-compare')[0];
	compare=($$('.mini-compare-products')!='')?'.mini-compare-products':'.block-compare';
	//compare=getE('.mini-compare-products','.block-compare');
	//alert(toplinkwish.title+":"+toplinkcart.title+":"+minicart+":"+miniwish+":"+compare);
}
// function getE(class1,class2){
		// if($$(class1)!=''){
			// //var eobject=$$(class1);
			// //return eobject[0];
			// return $$(class1)[0];
		// }
		// else if(class2!='' && $$(class2)!='') {
			// //var eobject=$$(class2);
			// return $$(class1)[0];		
		// }
		// else return false;
// }
function getToplinkwish(){
		//var la=$$('.links')[0].childElements();
		var la=$$('ul.links li a');
		//alert(la);
		//for(var i=0;i<la.length;i++){
			//la[i].childElements();
		//}
		for(var i=0;i<la.length;i++){
			if(la[i].href.search('/wishlist/')!=-1)
				return la[i];
		}
		//$$('.links').each(function (el){
		//	el.childElements().each(function(ew){
				//console.log("tag name"+ew.tagName);
				//for(x in ew){
				//console.log(x);
				//}
				//if(ew.href.search('/wishlist/')!=-1)
					//return ew;
				//	return "true ok";
			
			//});
		//});
		
		return false;
}
function init(){

	if(typeof productAddToCartForm != 'undefined'){		//kiem tra xem form productAddtoCartForm co ton tai hay ko
		productAddToCartForm.submit = function(args){
			if(this.validator && this.validator.validate()){
				url=$('product_addtocart_form').action;	//url cua form trong category view
				ajaxUpdate(url,'form');
			}
			return false;
		}		
	}
	updateDeleteLinks();
	updateRemoveWishLinks();
	if(islogin=="1"){
		updateWishlistLinks();
	}
	updateProductCompareLinks();
	updateRemovePCompareLinks();
	updateClearPCompareLinks();
	updateWishlistAddCartLinks();
	checkE();
}
	window.setPLocation= function(args){
		if (args.search('checkout/cart/add') != -1 || args.search('options=cart') != -1 ){
			if(setfocus){				//cho case xu ly tren win parent  ,,flag cho hoat dong la setfocus
				opener.isCompare=1;
				opener.focus();
            	opener.ajaxUpdate(args,'url');
			}
			else
				ajaxUpdate(args,'url');		//cho truong hop xu ly tren winpop
		}
		else if(args.search('wishlist/index/add')!=-1){
			if(opener.islogin!='0'){
				if(setfocus){
					opener.isCompare=1;
					opener.focus();
					opener.ajaxUpdate(args,'url');
				}
				else
					ajaxUpdate(args,'url');			
			}
			else{
				opener.focus();
				opener.location.href=args;
			}
		}
        else{
			opener.focus();
            opener.location.href = args;
        }		
	}
	var win="";
	window.popWin=function(url,newwin,para){
		win = window.open(url,newwin,para);
		win.focus();
	}
	window.onunload=function(){		//cho phep close win compare khi win parent redirect or refresh
		if(win){
		win.close();
		}
		//alert("aaaa");
	}	
// $$(".btn-cart").each(function()){
	// $$(".btn-cart").observe('click', function(){
			// var elem=this;
			// console.log(elem.tagName);
	// });


// $$(".btn-cart").observe( 'click', function( event )
// {
	  // var elem = event.element();
	  // if ( elem.match( 'input[type="button"]' ) )
	  // {
		// // Do your event handler
	  // }
// });

var deletePCompare=0;
	// window.removeItem=function(args){//alert(args);
		// if (args.search('catalog/product_compare/remove') != -1 ){opener.deletePCompare=1;
				// if(setfocus){				//cho case xu ly tren win parent  ,,flag cho hoat dong la setfocus
					
					// opener.isCompare=1;
					// opener.deletePCompare=1;

					// opener.focus();
					// opener.ajaxUpdate(args,'url');
					// exit;
				// }
				// else
					// ajaxUpdate(args,'url');		//cho truong hop xu ly tren winpop
		// }
        // else
        // {
            // window.location.href = args;
        // }	
	// };
	window.setLocation = function(args){
		// for(var x in this){
		// console.log(x);
		// }
		//console.log(this.name);
		//console.log(arguments.callee.caller.name);
		
		if(args.search('checkout/onepage')!=-1){	//neu click vao nut checkout trong sidebar cart
				window.location=args;
				return;
		}
		if(args.search('catalog/category')!=-1){	//neu click vao nut selecbox de sap xep theo ten va so luong san pham trong category
				window.location=args;
				return;
		}
        // if(window.location.href.match('https://') && !args.match('https://')){
            // args = args.replace('http://','https://')
        // }

		if (args.search('checkout/cart/add') != -1 || args.search('options=cart') != -1 || args.search('wishlist/index/cart')!=-1 || args.search('wishlist/index/cart')!=-1){
            ajaxUpdate(args,'url');
		}
		else if (args.search('catalog/product_compare/remove') != -1 ){opener.deletePCompare=1;
				if(setfocus){				//cho case xu ly tren win parent  ,,flag cho hoat dong la setfocus
					
					opener.isCompare=1;
					opener.deletePCompare=1;

					opener.focus();
					opener.ajaxUpdate(args,'url');
					exit;
				}
				else
					ajaxUpdate(args,'url');		//cho truong hop xu ly tren winpop
		}
        else
        {
            window.location.href = args;
        }
		
		//ajaxUpdate(args,'url');
			
			//alert("afkjdsfk");
			//$("process").setStyle({display:"block"});
			//tryToUpdateAjx();
			//var t=setTimeout("showConfirm()",3000);
			//var e=setTimeout("endConfirm()",5000);
		//adpShowMask('adpMask');adpShow('adpModal');return false;
	   //Performing checks
		//alert(args);
	 //  if(checkCondition(args)){

		 //Calling original funcs

	  //  org_foo(args);
	   //}
	};
var isdelete=0;	
//}
function addLayer(){
	str='<div id="adpMask_adpC" style="display: none; padding: 0px; position: fixed; left: 0px; top: 0px; width: 100%; height: 100%; z-index: 9000; visibility: visible;"><div style="width: 100%; height: 100%;background-color: #444444;opacity: 0.4;" id="ADPMask"></div></div>';
	return str;
}
// function addConfirm(){
// str='<div id="confirm" style="display:none;">';
	// str=str+'<DIV style="Z-INDEX: 10002; POSITION: fixed; PADDING-BOTTOM: 0px; BACKGROUND-COLOR: #2564c0; PADDING-LEFT: 0px; WIDTH: 252px; PADDING-RIGHT: 0px; DISPLAY: block; HEIGHT: 0px; VISIBILITY: visible; TOP: 158px; PADDING-TOP: 0px; LEFT: 584px" id=adpModal_adpB>';
		// str=str+'<!--block content-->';
		// str=str+'<DIV id="zconfirm" adpid="zconfirm" style="BORDER-BOTTOM: 0px inset; POSITION: absolute; BORDER-LEFT: 0px inset; PADDING-BOTTOM: 8px; BACKGROUND-COLOR: #ffc071; PADDING-LEFT: 8px; width: 260px; height: 104px; PADDING-RIGHT: 8px; DISPLAY: block; FONT: 10pt Arial;  VISIBILITY: visible; COLOR: #1c5ea2; OVERFLOW: hidden; BORDER-TOP: 0px inset; TOP: 21px; BORDER-RIGHT: 0px inset; PADDING-TOP: 8px; LEFT: 1px" >';
		// str=str+'	<p><span id="product_name">Nokia 2610 Phone</span> added to cart</p>';
		// str=str+'	<a class="focus" id="product_continue" href="#">Continue shopping (<span id="ACPcountdown">3</span>)</a>';
		// str=str+'	or	<a id="product_checkout" href="#">';
		// str=str+'	View cart &amp; checkout	</a>';
		// str=str+'	</DIV>';
	// str=str+'</DIV>	';
// str=str+'</div>';
	// return str;
// }
// function addProgress(){
	// str="<div id='process' style='display:none;'>";
	// str=str+"<DIV id='process-tab'>";
	// str=str+"	<!--block content-->";
	// str=str+"	<DIV  id='zprocess' adpid='adpModal'>";
	// //str=str+"		<CENTER>";
	// //str=str+"		<p id="loading_mask_loader" class="loader"><img alt="Loading..." src="<?php echo $this->getSkinUrl('mw_ajaxcart/images/ajax-loader-tr.gif')?>"><br>Please wait...</p>
// //	str=str+"		<div id='loading-mask'>";
	// //str=str+"			<p id='loading_mask_loader' class='loader'><img alt='Loading...' src='http://127.0.0.1/mage1411/skin/frontend/default/default/mw_ajaxcart/images/ajax-loader-tr.gif'><br>Please wait...</p>";
	// //str=str+"		<p> Loading </p>			";
	// //str=str+"		</CENTER>";
	// str=str+"      	</div>";
	// str=str+"	</DIV>";
	// str=str+"</DIV>	";
	// str=str+"</div>";
	// return str;
// }

function  updateDeleteLinks(){	
	var tmpLinks = document.links;
	for (i=0; i<tmpLinks.length; i++){
		//alert(tmpLinks[i].href);
		
		//if(i==52) {alert(tmpLinks[i].href);}
		if (tmpLinks[i].href.search('checkout/cart/delete') != -1){
				//alert(tmpLinks[i]);
				tmpLinks[i].onclick = function(e){
					e = e||event;
					if(e.preventDefault){
						e.preventDefault();		//for firefox
					}
					else{
						e.returnValue = false;		//for IE , prevent default
					}
					if(confirm('Are you sure you would like to remove this item from the shopping cart?')){
					isdelete=1;
					ajaxUpdate(this.href,'url');
					}
				}
		}
	}
}
function  updateRemoveWishLinks(){	
	var tmpLinks = document.links;
	for (i=0; i<tmpLinks.length; i++){
		//alert(tmpLinks[i].href);
		
		//if(i==52) {alert(tmpLinks[i].href);}
		if (tmpLinks[i].href.search('wishlist/index/remove') != -1){
				//alert(tmpLinks[i]);
				tmpLinks[i].onclick = function(e){
					e = e||event;
					if(e.preventDefault){
						e.preventDefault();		//for firefox
					}
					else{
						e.returnValue = false;		//for IE , prevent default
					}
					if(confirm('Are you sure you would like to remove this item from the your Wish List?')){
					isdelete=1;
					ajaxUpdate(this.href,'url');
					}
				}
		}
	}
}
function  updateRemovePCompareLinks(){	
	var tmpLinks = document.links;
	for (i=0; i<tmpLinks.length; i++){
		//alert(tmpLinks[i].href);
		
		//if(i==52) {alert(tmpLinks[i].href);}
		if (tmpLinks[i].href.search('catalog/product_compare/remove') != -1){
				children = tmpLinks[i].childNodes[0];
				//alert(tmpLinks[i].down('img').src); //IE ko ho tro ham down nhu firefox
				// children=tmpLinks[i].down('img');
			if(typeof(children.onclick)!='undefined'){	
				children.onclick = function(e){
					e = e||event;
					if(e.preventDefault){
						e.preventDefault();		//for firefox
					}
					else{
						e.returnValue = false;		//for IE , prevent default
					}				
				}	
			}	
				// var str="";
				// for(var x in children){
					// str=str+x+"||";
				// }
				// document.write(str);

				tmpLinks[i].onclick = function(e){
					e = e||event;
					if(e.preventDefault){
						e.preventDefault();		//for firefox
					}
					else{
						e.returnValue = false;		//for IE , prevent default
					}
					if(confirm('Are you sure you would like to remove this item from the Comparison List?')){
					isdelete=1;
					ajaxUpdate(this.href,'url');
					}
				}
			
		}
	}
}
function  updateClearPCompareLinks(){	
	var tmpLinks = document.links;
	for (i=0; i<tmpLinks.length; i++){
		//alert(tmpLinks[i].href);
		
		//if(i==52) {alert(tmpLinks[i].href);}
		if (tmpLinks[i].href.search('catalog/product_compare/clear') != -1){
				//alert(tmpLinks[i]);
				tmpLinks[i].onclick = function(e){
					e = e||event;
					if(e.preventDefault){
						e.preventDefault();		//for firefox
					}
					else{
						e.returnValue = false;		//for IE , prevent default
					}
					if(confirm('Are you sure you would like to remove all products from your comparison?')){
					isdelete=1;
					ajaxUpdate(this.href,'url');
					}
				}
		}
	}
}
function  updateWishlistLinks(){	
	var tmpLinks = document.links;
	for (i=0; i<tmpLinks.length; i++){
		//alert(tmpLinks[i].href);
		
		//if(i==52) {alert(tmpLinks[i].href);}
		if (tmpLinks[i].href.search('wishlist/index/add') != -1){
				//alert(tmpLinks[i]);
				tmpLinks[i].onclick = function(e){
					e = e||event;
					if(e.preventDefault){
						e.preventDefault();		//for firefox
					}
					else{
						e.returnValue = false;		//for IE , prevent default
					}

					//iswishlist=1;
					ajaxUpdate(this.href,'url');
					
				}
		}
	}
}
function  updateProductCompareLinks(){	
	var tmpLinks = document.links;
	for (i=0; i<tmpLinks.length; i++){
		//alert(tmpLinks[i].href);
		
		//if(i==52) {alert(tmpLinks[i].href);}
		if (tmpLinks[i].href.search('catalog/product_compare/add') != -1){
				//alert(tmpLinks[i]);
				tmpLinks[i].onclick = function(e){
					e = e||event;
					if(e.preventDefault){
						e.preventDefault();		//for firefox
					}
					else{
						e.returnValue = false;		//for IE , prevent default
					}

					//iswishlist=1;
					ajaxUpdate(this.href,'url');
					
				}
		}
	}
}
function  updateWishlistAddCartLinks(){	
	var tmpLinks = document.links;
	for (i=0; i<tmpLinks.length; i++){
		//alert(tmpLinks[i].href);
		
		//if(i==52) {alert(tmpLinks[i].href);}
		if (tmpLinks[i].href.search('wishlist/index/cart') != -1){
				//alert(tmpLinks[i]);
				tmpLinks[i].onclick = function(e){
					e = e||event;
					if(e.preventDefault){
						e.preventDefault();		//for firefox
					}
					else{
						e.returnValue = false;		//for IE , prevent default
					}

					//iswishlist=1;
					ajaxUpdate(this.href,'url');
					
				}
		}
	}
}
 //danh cho IE6, giup fix box giua man hinh
function scrollEvent() {
		var browser = navigator.appName; 
		var slidemenu_top='200px';
		var version = parseInt(navigator.appVersion); 
		if((browser == "Microsoft Internet Explorer") && (version<=6)){
			if (document.documentElement && !document.documentElement.scrollTop)
			// IE6 +4.01 but no scrolling going on
				var top =document.documentElement.scrollTop + parseInt(slidemenu_top)+"px";
			else if (document.documentElement && document.documentElement.scrollTop)
			// IE6 +4.01 and user has scrolled
				var top =document.documentElement.scrollTop + parseInt(slidemenu_top)+"px";
			else if (document.body && document.body.scrollTop)
				var top =document.body.scrollTop + parseInt(slidemenu_top)+"px";
			//alert(document.body.scrollTop);
			//console.log(top);
			document.getElementById("options-tab").style.top = top; 
			//document.getElementById("fixbox").style.top = top; 
		}			
} 
function fixcenter(){
	var theWidth, theHeight;
	// Window dimensions:
	if (window.innerWidth) {
	theWidth=window.innerWidth;
	}
	else if (document.documentElement && document.documentElement.clientWidth) {
	theWidth=document.documentElement.clientWidth;
	}
	else if (document.body) {
	theWidth=document.body.clientWidth;
	}
	if (window.innerHeight) {
	theHeight=window.innerHeight;
	}
	else if (document.documentElement && document.documentElement.clientHeight) {
	theHeight=document.documentElement.clientHeight ;
	}
	else if (document.body) {
	theHeight=document.body.clientHeight;
	}
	var midheight=(parseInt(theHeight/2)-parseInt($('zoptions').getHeight()/2))+'px';
	//alert(midheight+"="+parseInt(theHeight/2)+"-"+parseInt($('zoptions').getHeight()/2));
	$('options-tab').setStyle({top:midheight});
	//return new Array(theWidth,theHeight);
}
function addOptionscart(){
	//var newar=fixcenter();
	// for(var i=0;i<newar.length;i++){
		// alert(newar[i]);
	// }
//	var midwidth=parseInt(newar[1]/2)-parseInt($('zoptions').getHeight());
	var midwidth=0;
	str='		<div id="options">';
	str=str+'		<DIV id="options-tab" style="top:'+midwidth+'px;">';
	str=str+'			<!--block content-->';
	str=str+'			<DIV id="zoptions" adpid="adpModal">';
	str=str+'					<div id="process" style="display:block;"></div>							';
	str=str+'					<div id="fancybox-wrap" style="display:none;"><div id="fancybox-outer"><div id="fancy-bg-n" class="fancy-bg"></div><div id="fancy-bg-ne" class="fancy-bg"></div><div id="fancy-bg-e" class="fancy-bg"></div><div id="fancy-bg-se" class="fancy-bg"></div><div id="fancy-bg-s" class="fancy-bg"></div><div id="fancy-bg-sw" class="fancy-bg"></div><div id="fancy-bg-w" class="fancy-bg"></div><div id="fancy-bg-nw" class="fancy-bg"></div><div id="confirmbox"></div>';
	str=str+'					<a id="fancybox-close" style="display: inline;" onclick="$(\'confirmbox\').innerHTML=\'\';$(\'fancybox-wrap\').setStyle({display:\'none\'});$(\'options\').setStyle({display:\'none\'});"></a></div></div>';
	str=str+'			</DIV>';
	str=str+'		</DIV>	';
	str=str+'	</div>';
	//str=str+'<div id="fixbox" style="Z-INDEX: 10002; POSITION: fixed;height:100px;width:100px;DISPLAY: block; TOP: 158px; PADDING-TOP: 0px; LEFT: 584px">This box has its position fixed applied to it. For IE6, it is set to absolute positioning using the * html hack and works just like fixed positioning.The "View the CSS" and "learn about using it" links to the right are also absolutely positioned, but they dont behave like a fixed element in IE6.</div>';
	return str;
	//onclick="$(\'confirmbox\').innerHTML=\'\';$(\'fancybox-wrap\').setStyle({display:\'none\'});$(\'options\').setStyle({display:\'none\'});"
	//onclick="$(\'options\').setStyle({display:\'none\'});$(\'fancybox-wrap\').setStyle({display:\'none\'});$(\'confirmbox\').innerHTML=\'\';"	
}

function initfunc(){
	init();
	//myElement="adjkfj kjdf jkajf kjkdsfj lksafj";
	//myElement.appendToTop('body');
	//alert("asdas");
	//Effect add to cart
	 // $$('.btn-cart').each(function (el){		//select class name va thao tac voi no’
			   // el.observe('click', function(event){
					// btelem=event.element();//console.log(elem.className);
					// tempe=btelem.up('li').down('a').down('img');
					// if(tempe!=undefined)
					// elem=tempe;
				// //	for(var x in elem){console.log(x);}
			   // });
	 // });
	var s=addOptionscart()+addLayer();
	var f = $$('body');
	var t = $$('.wrapper');
		f[0].insert({top:s});
		
		//window.onscroll = scrollEvent("fixbox");
		//window.onscroll = scrollEvent();
		//window.onscroll = scrollEvent();
		var browser = navigator.appName; 
		var version = parseInt(navigator.appVersion); 
		if((browser == "Microsoft Internet Explorer") && (version<=6)){
			window.onscroll = scrollEvent
			//document.getElementById("fixbox").style.position = 'absolute'; 
			document.getElementById("options-tab").style.position = 'absolute'; 
		}
		//tuong duong f[0].insert(new Element('p')).down().update('This is myinserted paragraph');;

		//for(var i=0; i<f.length; i++){
			//f[0].setStyle({display:'none'});;
		//}
		//alert(f.length); // shows: "joedoe1/secret/"


}
window.onload=initfunc;
