jQuery.noConflict();
var ie6=(jQuery.browser.msie && jQuery.browser.version < 7),d=document,de=d.documentElement,w=window;
function byID(e){return d.getElementById(e);}
/* heavily modified Thickbox 3.1
 * Original By Cody Lindley (http://www.codylindley.com)
 */
function tb_init(s){
//	jQuery(s||'a.thickbox,area.thickbox,input.thickbox').click(								//jQuery<=1.3.x
	jQuery('body').delegate(s||'a.thickbox,area.thickbox,input.thickbox','click',		//jQuery>=1.4
	function(){
		tb_show(this.title||this.name||"",this.href||this.alt,this.rel||false);
		this.blur();
		return false;
	});
}

function tb_show(caption,url,grp){
	jQuery("#TB_Image").remove();
	if(ie6){
		TB_SaveWidth=jQuery("body").css("width");
		jQuery("body","html").css({height:"100%",width:"100%",overflow:"hidden"});
		if (byID("TB_hs")===null)
			jQuery("body").append("<iframe id='TB_hs'></iframe>");
	}
	if(byID("TB_o")===null){
		jQuery("body").append('<div id="TB_o"></div><div id="TB_w"><div id="TB_title"><span></span><div id="TB_ctrl"></div></div></div><div id="TB_load"></div>');
		jQuery("#TB_o").click(tb_remove);
	}
	jQuery('#TB_title span:first-child').html(caption);
	var baseURL=url.split('?')[0].toLowerCase(),
		TB_w=jQuery("#TB_w"),
		TB_o=jQuery("#TB_o"),
		TB_ctrl='<img id="TB_close" src="s/tb-close.png" onclick="tb_remove();" alt="Close">';

	//group processing like original thickbox imagegroup
	if(grp){
		var n=false,
			a=jQuery("a[rel="+grp+"]").get();
		for(var i=0;i<a.length;i++){
			if(a[i].href==url){
				var p=(i?i:a.length)-1,
					n=(++i==a.length)?0:i;
				TB_ctrl='Page '+(i)+' of '+a.length+'<img id="TB_prev" src="s/tb-prev.png" onClick="tb_show(\''+(a[p].title||grp)+"','"+a[p].href+"','"+grp+'\');return false;" alt="&lt; Prev"><img id="TB_Next" src="s/tb-next.png" onClick="tb_show(\''+(a[n].title||grp)+"','"+a[n].href+"','"+grp+'\');return false;" alt="Next &gt;">'+TB_ctrl;
			}
		}
	}
	
	if(/\.jpg$|\.jpeg$|\.png$|\.gif$|\.bmp$/.test(baseURL)){//code to show images
		P=new Image();
		P.onload=function(){
			P.onload=null;
			var x=(w.innerWidth||de.clientWidth)-60,
				y=(w.innerHeight||de.clientHeight)-60,
				iW=P.width,
				iH=P.height;
			if(iH>y){iW=iW*y/iH;iH=y;}
			if(iW>x){iH=iH*x/iW;iW=x;}
			TB_w.width(iW+2).height(iH+31).append('<img id="TB_Image" src="'+url+'" width="'+iW+'" height="'+iH+'" alt="'+caption+'" onclick="tb_remove();"/>');
			d.onkeydown=function(e){ 	
				var k=e?e.which:event.keyCode;
				if(k==27){
					tb_remove();
				}else if(k==188||k==37){ // '<', left
					d.onkeydown="";
					jQuery('#TB_Prev').click();
				}else if(k==190||k==39){ // '>', right
					d.onkeydown="";
					jQuery('#TB_Next').click();
				}
			};
			tb_loaded();
		};
		P.src=url;
	}else{//code to show html
		var params=tb_parseQuery(url.replace(/^[^\?]+\??/,''));
		var ajaxCW=params.width||770,
			ajaxCH=params.height||455;
		TB_w.width(ajaxCW+30).height(ajaxCH+45);
		if(params.modal=='true'){
			TB_ctrl='';
			//jQuery("#TB_close").remove();
			TB_o.unbind();
		}
		if(url.indexOf('TB_iframe') !=-1){// iframe
			var urlNoQuery=url.split('TB_');
			jQuery("#TB_iframeContent").remove();
			TB_w.append("<iframe frameborder='0' hspace='0' src='"+urlNoQuery[0]+"' id='TB_iframeContent' name='TB_iframeContent"+Math.round(Math.random()*1000)+"' onload='tb_loaded(1)' style='width:"+(ajaxCW+29)+"px;height:"+(ajaxCH+17)+"px;'> </iframe>");
		}else{// ajax
			if(TB_w.css("display")!="block"){
				TB_w.append("<div id='TB_c'"+((params.modal=="true")?" class='TB_modal'":"")+" style='width:"+ajaxCW+"px;height:"+ajaxCH+"px;'></div>");
			}else{//this means the window is already up, we are just loading new content via ajax
				//jQuery("#TB_c")[0].css({scrollTop:0,width:ajaxCW+'px',height:ajaxCH+'px'});
				var c=jQuery("#TB_c")[0];
				c.width(ajaxCW).height(ajaxCH).css('scrollTop',0);
//					jQuery("#TB_title span").html(caption);
			}
		}
		var TB_c=jQuery("#TB_c");
		if(url.indexOf('TB_inline') !=-1){	
			TB_c.append(jQuery('#'+params.inlineId).children());
			TB_w.unload(function (){
				jQuery('#'+params.inlineId).append(TB_c.children()); // move elements back when you're finished
			});
			tb_loaded();
		}else if(url.indexOf('TB_iframe') !=-1){
			if(jQuery.browser.safari)
				tb_loaded(1);
		}else{
			if(/\.swf$/.test(baseURL)){
				TB_c.html("<object type=\"application/x-shockwave-flash\" data=\""+url+"\" width=\""+params.width+"\" height=\""+params.height+"\"><param name=\"movie\" value=\""+url+"\"/><param name=\"allowScriptAccess\" value=\"always\"/></object>");
				tb_loaded();
			}else if(/\.pdf$/.test(baseURL)){
				TB_c.html("<embed type=\"application/pdf\" src=\""+url+"\" width=\"100%\" height=\"100%\"></embed>");
				tb_loaded();
			}else{
				url+=url.indexOf('?')!=-1?'&':'?';
				TB_c.load(url+="random="+new Date().getTime(),'',tb_loaded(1));
			}
		}
		if(!params.modal)
			d.onkeyup=function(e){
				if ((e?e.which:event.keyCode)==27)
					tb_remove();
			};
	}

	function tb_loaded(flag){
		jQuery('#TB_ctrl').html(TB_ctrl);
		wcenter();
		if(flag)tb_init("#TB_c .thickbox");
		jQuery("#TB_load").hide();
		jQuery("#TB_w").css({display:"block"});
	}
}

function tb_remove(){
	jQuery("#TB_close").unbind("click");
	jQuery("#TB_w,#TB_load").fadeOut("fast",function(){jQuery('#TB_w,#TB_o,#TB_hs,#TB_load').trigger("unload").unbind().remove();});
	if(ie6){
		jQuery("body","html").css({height:"auto",width:"auto",overflow:""});
		jQuery("body").css("width",TB_SaveWidth);
	}
	d.onkeydown="";
	d.onkeyup="";
	return false;
}

function wcenter(s){
	var W=jQuery(s||"#TB_w");
	W.css({marginLeft:'-'+(W.width()>>1)+'px',marginTop:'-'+(W.height()>>1)+'px'});
}

function tb_parseQuery(q){
	var P={},k;
	if(q){
		q=q.split(/[;&]/);
		for(var i=0;i<q.length;i++){
			k=q[i].split('=');
			if(!k||k.length!=2)continue;
			P[unescape(k[0])]=unescape(k[1]).replace(/\+/g,' ');
		}
	}
	return P;
}
//end thickbox
function addCart(d){
	if(!byID('popmsg'))
		jQuery('body').append('<div id="popmsg"><a onclick="jQuery(\'#popmsg\').hide()">close</a><div></div></div>');
	setTimeout(function(){jQuery('#popmsg div').load('index2.php',d,addCartCallback());},100);
	return false;
}
function addCartCallback(){
	wcenter('#popmsg');
	jQuery('#popmsg').show('fast');
	setTimeout(function(){jQuery('#hdrcart').html(jQuery('#newCartTotal').html());},100);
	setTimeout(function(){jQuery('#popmsg').hide('slow');},4000);
	return false;
}
function pageStart(){}
var m_id,m_to;
function mShow(id){
	if(m_id)mHide();
	if(m_to)clearTimeout(m_to);
	m_id=id;
	var m=jQuery('#'+id),s=jQuery('#'+id+'s'),u=jQuery('#menu').position(),h=jQuery('#hdr').width();
	var p=m.position();
	var l=(u.left+p.left+25)+'px',
		t=(jQuery('#hdr').height()-5)+'px';
	if(s.width()+l>h)l=h-s.width();
	s.show().css({left:l,top:t});
}
function mHide(){jQuery('#'+m_id+'s').hide();m_id='';}
function mExit(id){m_to=setTimeout(mHide,1000);}
