/*
JavaScript Gallery v1.4
Copyright (C) 2008 Aptus s.c.
All rights reserved
Change date: 15.06.2009
*/

var jsgId='JSgallery';
var jsgImgPath='/i/common/';
var jsgDefaultId='jsg';

var jsgActive=null;
var jsgGalleries=[];
var jsgCallback=false;
var jsgSrc;

function jsgMove()
{
	var e=document.getElementById(jsgId);
	if(e)
		e.style.top = document.documentElement.scrollTop + 'px';	
}

function jsgOnScroll(e)
{
		jsgMove();
}

function jsgResize()
{
	var ew = 0;
	var eh = 0;
	if(typeof(window.innerWidth) == 'number'){
		ew = window.innerWidth;
		eh = window.innerHeight;
	}
	else if(document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)){
		ew = document.documentElement.clientWidth;
		eh = document.documentElement.clientHeight;
	}
	else if(document.body && (document.body.clientWidth || document.body.clientHeight)){
		ew = document.body.clientWidth;
		eh = document.body.clientHeight;
	}
	
	var e=document.getElementById(jsgId);
	if(e) {
		var es=e.style;
		if(es.display!='none') {
			es.display='none';
			es.width=ew+'px';
			es.height=eh+'px';
			es.display='';
		}
	}
}

function jsgOnResize(e)
{
	jsgResize();
}


function jsgLoad(e)
{
	jsgMove();
}

function jsgImgClick(e)
{
	var img=document.getElementById(jsgId+'_image');
	if(typeof document.documentElement != 'undefined' && typeof document.documentElement.clientWidth != 'undefined' && document.documentElement.clientWidth != 0)
		var ew = document.documentElement.clientWidth;
	else
		var ew = document.getElementsByTagName('body')[0].clientWidth;
	var x=e ? e.pageX : event.clientX;
	if(x<ew/2)
		jsgActive.prev();
	else
		jsgActive.next();
}

function jsgImgOver()
{
	document.getElementById(jsgId+'_navcntr').style.display = 'block';
}

function jsgImgOut()
{
	document.getElementById(jsgId+'_navcntr').style.display = 'none';
}

function jsgInit()
{
	var e=document.getElementById(jsgId);
	if(!e) {
		window.onresize=jsgOnResize;
		var e=document.createElement('div');
		e.id=jsgId;
		try{e.style.position = 'fixed';} catch(err){e.style.position='absolute';};
		var browser_ver = parseInt(navigator.appVersion.substring(navigator.appVersion.indexOf("MSIE")+5,navigator.appVersion.length));
		if(navigator.appName=="Microsoft Internet Explorer" && browser_ver<=6)
			e.style.position='absolute';
		if(e.style.position=='absolute')
			window.onscroll=jsgOnScroll;
		e.style.left='0px';
		e.style.top='0px';
		var bg='<!--[if lte IE 6]><iframe style="position: absolute; top: 0; left: 0; height: 100%; width:100%; border: 0; background: #000; filter: alpha(opacity=50)"></iframe><![endif]-->';
		if(typeof e.style.opacity!='undefined')
			bg +='<div style="background-color:#000; opacity:0.6; z-index:9995; position:absolute; width:100%; height:100%;"></div>';		
		else if(typeof e.style.filter!='undefined')
      bg +='<div style="background-color:#000; filter:progid:DXImageTransform.Microsoft.Alpha(opacity=60); z-index:9995; position:absolute; width:100%; height:100%;"></div>';    
		else			
			e.style.backgroundImage='url('+jsgImgPath+'jsg_bg.gif)';
		e.innerHTML=bg+'<table style="position:relative;width:100%;height:100%;z-index:9999;" id="'+jsgId+'_window"><tr><td style="vertical-align:middle;text-align:center;"><table style="margin:auto;text-align:left;" cellspacing=0 cellpadding=0 class="jsg"><tr><td class="jheader"><a href="javascript:jsgClose();void(0);"><img src="'+jsgImgPath+'jsg_close.gif"></a><span id="'+jsgId+'_title"></span></td></tr><tr><td class="image"><div style="position:relative;" id="'+jsgId+'_image_container"><img src="" id="'+jsgId+'_image"><div style="position:absolute;width:100%;top:20%;left:0;display:none;" class="navcntr" id="'+jsgId+'_navcntr"></div></div></td></tr><tr><td class="jfooter" id="'+jsgId+'_footer"><div class="navi" id="'+jsgId+'_navi"></div><span id="'+jsgId+'_desc"></span></td></tr></table></td></tr></table>';
		document.body.appendChild(e);
		e=document.getElementById(jsgId);
		var img=document.getElementById(jsgId+'_image');
		//if(img.addEventListener && e.style.position=='absolute')
			//img.addEventListener('load',jsgLoad,false);
		var img_cont = document.getElementById(jsgId+'_image_container');
		img_cont.onclick=jsgImgClick;			
		img_cont.onmouseover=jsgImgOver;
		img_cont.onmouseout=jsgImgOut;		
	}
	if(e.style.position=='absolute')
		jsgMove();
	e.style.display='';	
}

function jsgClose()
{
	var e=document.getElementById(jsgId);
	if(e) {
		e.style.display='none';
		document.getElementById(jsgId+'_image').src='';
	}
	jsgActive=null;
}

function jsgShow(i)
{
	this.current=i;
	var img=this.images[i];	
	document.getElementById(jsgId+'_image').src=typeof(img)=='object' ? img[0] : img;		
	var desc=(this.title.length>0 || this.images.length>1) ? (i+1)+'/'+this.images.length+' '+(typeof(img)=='object' ? img[1] : '') : '';
	var title=(this.title.length>0 || this.images.length>1) ? this.title : typeof(img)=='object' ? img[1] : '';
	var navi = this.images.length>1 ? '<a href="javascript:jsgActive.first();void(0);"><img src="'+jsgImgPath+'jsg_first.gif"></a><a href="javascript:jsgActive.prev();void(0);"><img src="'+jsgImgPath+'jsg_prev.gif"></a><a href="javascript:jsgActive.next();void(0);"><img src="'+jsgImgPath+'jsg_next.gif"></a><a href="javascript:jsgActive.last();void(0);"><img src="'+jsgImgPath+'jsg_last.gif"></a>' : '';
	var navcntr = '';
	if(this.images.length>1){
		navcntr += i!==0 ? '<img style="float:left;border-left:none;" src="'+jsgImgPath+'jsg_arrowl.gif">' : '';
		navcntr += i!=this.images.length-1 ? '<img style="float:right;border-right:none;" src="'+jsgImgPath+'jsg_arrowr.gif">' : '';
	}
	
	document.getElementById(jsgId+'_navi').innerHTML = navi;
	document.getElementById(jsgId+'_navcntr').innerHTML = navcntr;
	document.getElementById(jsgId+'_desc').innerHTML = desc;
	//document.getElementById(jsgId+'_title').innerHTML = title;
	
	window.setTimeout(jsgResize,100); //ie6, safari fix
}

function jsgOpen(i)
{
	jsgInit();
	jsgActive=this;	
	this.show(i);
}

function jsgFirst()
{
	this.show(0);
}

function jsgPrev()
{
	if(this.current>0)
		this.show(this.current-1);
}

function jsgNext()
{
	if(this.current<this.images.length-1)
		this.show(this.current+1);
}

function jsgLast()
{
	this.show(this.images.length-1);
}

function jsgFind(i)
{
	var c;
	for(c=0; c<this.images.length; c++)
		if(i==(typeof(this.images[c])=='object' ? this.images[c][0] : this.images[c]))
			return c;
	return null;
}

function JSgallery(g,t)
{
	this.images=g;
	this.title=t;
	this.open=jsgOpen;
	this.show=jsgShow;
	this.first=jsgFirst;
	this.prev=jsgPrev;
	this.next=jsgNext;
	this.last=jsgLast;
	this.find=jsgFind;
	this.move=true;
}

function jsgFindImages(e,d)
{
	var img=new Array();
	var i,j;
	for(i=0; i<e.childNodes.length; i++)
		if(e.childNodes[i].tagName) {
			if(e.childNodes[i].tagName=='IMG' && e.childNodes[i].onclick) {
				e.childNodes[i].onclick();
				img.push([jsgSrc,e.childNodes[i].title]);
			}
			else if(e.childNodes[i].tagName=='A' && e.childNodes[i].id==d) {
				img.push([e.childNodes[i].href,e.childNodes[i].title]);
			}
			var img2=jsgFindImages(e.childNodes[i],d);
			for(j=0; j<img2.length; j++)
				img.push(img2[j]);
		}
	return img;
}

function jsgClick(i,e,d)
{
	var l=false;
	var jsgTmp;
	if(jsgCallback) {
		jsgSrc=i;
		return;
	}
	if(typeof(i)=='object') {


		d=e;
		e=i;
		i=i.href;
	}
	if(typeof(e)=='object')
		l=true;
	else
		d=e;
	if(typeof(d)=='undefined')
		d=jsgDefaultId;
	if(l || typeof(jsgGalleries[d])=='undefined') {
		var bl;
		if(l) {
			var eh=e;
			while(e) {

				if(e.id==d) {
					bl=e;
					break;
				}
				e=e.parentNode;
			}
			if(!bl) {
				jsgTmp=new JSgallery([i],eh.title);
				jsgTmp.single=true;
				jsgTmp.open(0);
				return false;
			}
		}
		else
			bl=document.getElementById(d);
		if(!bl)
			return;
		jsgCallback=true;
		var img=jsgFindImages(bl,d+'i');
		jsgCallback=false;
		jsgTmp=new JSgallery(img,bl.title);
		if(!l)
			jsgGalleries[d]=jsgTmp;
	}
	else
		jsgTmp=jsgGalleries[d];
	jsgTmp.open(jsgTmp.find(i));
	return false;
}

function img1(name) {
	 img=new Image();
     img.src = '/i/'+name+'.gif';
	 document.images[name].src=img.src;
}

function img2(name) {
	 img=new Image();
     img.src = '/i/'+name+'b.gif';
	 document.images[name].src=img.src;
}

function dbl(t) {
	if(t.dlg_clicked)
		return false;
	t.dlg_clicked=true;
	return true;
}

function input_click(inp,pass){
	inp.value="";
	if(inp.className.length>0)
		inp.className="";
}

function getElementsByClassName(cn){
  var arr = new Array(); 
  var els = document.getElementsByTagName("*");
  var exp= new RegExp("^(.* )?"+cn+"( .*)?$", "g");
  for (var i = 0; i < els.length; i++ ){
    if (exp.test(els[i].className)){
      arr.push(els[i]);
    }
  }
  return arr;
}

function submitSticker()
{
	num=0;
	while(document.getElementById('line_'+num+'_div')) {
		if(document.form.elements['line_'+num].value.length==0)
			document.getElementById('line_'+num+'_div').style.color='red';
		else
			document.getElementById('line_'+num+'_div').style.color='#643265';
		num++;
	}
	document.form.target="preview";
	document.forms['form'].attributes['action'].value = 'sticker.php?send=1';
	document.form.submit();
	document.getElementById("iframe_preview").style.display="none";
	document.getElementById("loaderDiv").style.display="block";
}

function afterLoad(obj)
{
	obj.style.display="block";
	//document.getElementById("iframe_preview").style.display="none";
	document.getElementById("loaderDiv").style.display="none";
}

function submitCart()
{
	document.form.target="";
	document.forms['form'].attributes['action'].value = 'product.php';
	document.form.submit();
}

function dlg_url(u)
{
	if(typeof(u)=='object') {
		var s=u[0];
		if(typeof(u[1])=='object') {
			var k;
			var sp='?';
			for (k in u[1]) {
				s+=sp+encodeURIComponent(k)+'='+encodeURIComponent(u[1][k]);
				sp='&';
			}
		}
		if(typeof(u[2])!='undefined')
			s+='#'+u[2];
		return s;
	}
	return u;
}

function dlg_get(u,cb,cbp)
{
	return dlg_xmlhttprequest(u,'GET','','',cb,cbp);
}

function dlg_xmlhttprequest(u,m,d,c,cb,cbp)
{
	u=dlg_url(u);
	var r=false;
    if(window.XMLHttpRequest) {
    	try {
			r=new XMLHttpRequest();
        }
		catch(e) {
			r=false;
        }
    }
	else if(window.ActiveXObject) {
       	try {
        	r=new ActiveXObject("Msxml2.XMLHTTP");
      	}
		catch(e) {
        	try {
          		r=new ActiveXObject("Microsoft.XMLHTTP");
        	}
			catch(e) {
          		r=false;
        	}
		}
    }

	if(r) {
		r.onreadystatechange=function() {
			if(r.readyState==4) {
				if(r.status==200)
					cb(r.responseText,cbp);
			}
		};
		r.open(m,u);
		if(c!='')
			r.setRequestHeader('Content-Type',c);
		r.send(d);
		return true;
	}
	return false;
}

function changeProductPrice(id_product)
{
	var id_variant=document.form.id.value;
	dlg_get(['product.php', {'action':'variant_price', 'id_product':id_product, 'id_variant':id_variant}], function(d) { document.getElementById('productPrice').innerHTML=d; } );
}

function changeProductPic(id)
{
	document.getElementById('pictureDiv').innerHTML='<img src="img.php?sticker_color='+id+'"></img>';
}

function colorSwitch(obj,v) {
	var objs = getElementsByClassName('frameHover');
	if(objs.length>0) {
		for(var i = 0; i<objs.length; i++) {
			objs[i].className = 'frame';
		}
	}
	obj.className = 'frameHover';
	document.getElementById('color_input').value=v;
	document.form.color.value=v;
}

