var cenaText = '<strong>CENÍK</strong><br/><h2 class="tth2">Vyvolání fotografií :</h2><ul class="ttul"><li>13 x 18 .......... 40,-Kč</li><li>15 x 21 .......... 55,-Kč</li><li>20 x 30 .......... 250,- Kč</li><li>30 x 40 .......... 500,- Kč</li><li>40 x 60 .......... 700,- Kč</li><li>50 x 70 .......... 1000,- Kč</li><li>60 x 80 .......... 1300,- Kč</li></ul><h2 class="tth2">Vypálení fotografií na CD :</h2><p> Za každou fotografii si účtuji  100,- Kč </p>';

var tooltip=function(){
	var id = 'tt';
	var top = 3;
	var left = 3;
	var maxw = 300;
	var speed = 10;
	var timer = 20;
	var endalpha = 95;
	var alpha = 0;
	var tt,t,c,b,h;
	var ie = document.all ? true : false;
	return{
		show:function(v,w){
			if(tt == null){
				tt = document.createElement('div');
				tt.setAttribute('id',id);
				t = document.createElement('div');
				t.setAttribute('id',id + 'top');
				c = document.createElement('div');
				c.setAttribute('id',id + 'cont');
				b = document.createElement('div');
				b.setAttribute('id',id + 'bot');
				tt.appendChild(t);
				tt.appendChild(c);
				tt.appendChild(b);
				document.body.appendChild(tt);
				tt.style.opacity = 0;
				tt.style.filter = 'alpha(opacity=0)';
				document.onmousemove = this.pos;
			}
			tt.style.display = 'block';
			c.innerHTML = v;
			tt.style.width = w ? w + 'px' : 'auto';
			if(!w && ie){
				t.style.display = 'none';
				b.style.display = 'none';
				tt.style.width = tt.offsetWidth;
				t.style.display = 'block';
				b.style.display = 'block';
			}
			if(tt.offsetWidth > maxw){tt.style.width = maxw + 'px'}
			h = parseInt(tt.offsetHeight) + top;
			clearInterval(tt.timer);
			tt.timer = setInterval(function(){tooltip.fade(1)},timer);
		},
		pos:function(e){
			var u = ie ? event.clientY + document.documentElement.scrollTop : e.pageY;
			var l = ie ? event.clientX + document.documentElement.scrollLeft : e.pageX;
			tt.style.top = (u - h) + 'px';
			tt.style.left = (l + left) + 'px';
		},
		fade:function(d){
			var a = alpha;
			if((a != endalpha && d == 1) || (a != 0 && d == -1)){
				var i = speed;
				if(endalpha - a < speed && d == 1){
					i = endalpha - a;
				}else if(alpha < speed && d == -1){
					i = a;
				}
				alpha = a + (i * d);
				tt.style.opacity = alpha * .01;
				tt.style.filter = 'alpha(opacity=' + alpha + ')';
			}else{
				clearInterval(tt.timer);
				if(d == -1){tt.style.display = 'none'}
			}
		},
		hide:function(){
			clearInterval(tt.timer);
			tt.timer = setInterval(function(){tooltip.fade(-1)},timer);
		}
	};
}();

function getFinalPrice()
{
	var price = 0;
	var text = '';
	var sformat = document.getElementsByTagName('select');
	var finalPrice = document.getElementById('finalPrice');
	
	for (i=0; i < sformat.length; i++){
		text = sformat[i][sformat[i].selectedIndex].text; //alert(sformat[i][sformat[i].selectedIndex].text);
		t_arr = text.split(' ');
		if (!isNaN(parseInt(t_arr[t_arr.length-1]))) price += parseInt(t_arr[t_arr.length-1]); //alert(parseInt(t_arr[t_arr.length-1]));
	}
	finalPrice.innerHTML = formatCena(price) + 'Kč';
}
function price(cena)
{	
	var fc = document.getElementById('final_cena');
	if (cena < 1000){		
		var platba = document.getElementsByName('platba');
		var doprava = document.getElementsByName('doprava');
		
		if (platba[0].checked){ //bankovni prevod
			if (doprava[0].checked) cena += Cena_Posta;
			doprava[0].disabled = false;
			doprava[1].disabled = false;
		}
		else if (platba[1].checked){ //dobirka
			doprava[0].checked = true;
			doprava[1].disabled = true;
			doprava[0].disabled = false;
			//if (doprava[2].checked) doprava[0].checked = true;
			if (doprava[0].checked) cena += Cena_Posta_Dob;
			//if (doprava[1].checked) cena += Cena_PPL_Dob;					
			//doprava[1].disabled = false;
			
		}
		else if (platba[2].checked) { //hotove
			doprava[1].checked = true;
			doprava[0].disabled = true;
			doprava[1].disabled = false;
			//doprava[2].disabled = false;

			//if (doprava[0].checked) cena += Cena_Posta;
			//if (doprava[1].checked) cena += Cena_PPL;
		}
	}
	fc.innerHTML = '<strong>'+formatCena(cena)+' Kč</strong>';
	
}
function formatCena(cena)// id, poradi, nazev
{
	var ar = str_split(cena,1); 
	var arr = array_reverse(ar); 
	var out = ''; 
	for (var x=0; x < ar.length; x++){ 
		out += arr[x]; 
		out += (((x+1) % 3) == 0) ? ' ' : '';
	}
	//alert(out);
	out = strrev(out); //.'<span class="mini"> Kč</span>';
	if (substr(out,0,1) == ' ') out = substr(out,1);
	return out;
}
function checkzasilani(el){
	var check = document.getElementsByName('email_zasilani[]');
	if (check[0] == el && check[0].checked) {
		for(i=1; i<5; i++){
			check[i].checked = true; 	
		}
	}
	else if (check[0] == el && !check[0].checked){
		for(i=1; i<5; i++){
			check[i].checked = false; 	
		}
	}
	else if (check[0] != el) check[0].checked = false;
	
}
function switchStars(n) {
	for(var i=1; i<=5; i++){
		var star = document.getElementById('star'+i);
		if (i<=n){
			star.className = 'staractive';
		}
		else star.className = 'star';
	}
}
function resetStars(){
	for(var i=1; i<=5; i++){
		var star = document.getElementById('star'+i);
		star.className = 'star';
	}
}
function toggleContent(name,n) {
	var i,t='',el = document.getElementById(name);
	if (!el.origCont) el.origCont = el.innerHTML;
	
	for (i=0;i<n;i++) t += el.origCont;
	el.innerHTML = t;
}

function menu_switch(e,ev) {//alert(e.parentNode.parentNode.id);
	if(e.nodeName == 'LI'){
		var d = new Date();
		d.setTime(d.getTime()+(15*60*1000));
		var trida; var ext = '';
		trida = e.className; 
		arr = trida.split(' ');
		trida = arr[0];
		for (i=1; i<arr.length; i++){
			ext += ' '+arr[i];
		}
		// disable bubbling
		if (window.event) {
			ev.cancelBubble = true;
		}
		else {
			ev.stopPropagation();
		}
   
		if ((trida=="close" || trida=="open")) { 
   	  		e.className = (trida=="open") ? "close"+ext : "open"+ext; 
			if (trida == "close") {document.cookie = e.id+'='+e.id+'; expires='+d.toUTCString()+'; path=/'; }
			else document.cookie = e.id+'=false; expires=Fri, 27 Jul 2001 02:47:11 UTC; path=/';
			return false;
  		} 
	}
	else
	return true;
} 
function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}
function setCookie(name,val,hours) {
	var d = new Date();
	d.setTime(d.getTime()+(hours*60*1000));
	if (hours)
		document.cookie = name+'='+val+'; expires='+d.toUTCString()+'; path=/';
	else
		document.cookie = name+'='+val+'; expires=Fri, 27 Jul 2001 02:47:11 UTC; path=/';
	return false;	
}
function getStyle(el,styleProp) 
{ 
var x = document.getElementById(el); 
if (x.currentStyle) 
var y = x.currentStyle[styleProp]; 
else if (window.getComputedStyle) 
var y = document.defaultView.getComputedStyle(x,null).getPropertyValue(styleProp); 
return y; 
} 

function switchClass(id1,class1,id2,class2)
{
	var el1; el1 = document.getElementById(id1);
	var el2; el2 = document.getElementById(id2);
	
	el1.className = 'quotes '+class1 ; //alert (el1.className);
	el2.className = 'quotes '+class2;
	
	return false;
}

function str_split ( f_string, f_split_length){
    f_string += '';

    if (f_split_length == undefined) {
        f_split_length = 1;
    }
    if (f_split_length > 0) {
        var result = [];
        while (f_string.length > f_split_length) {
            result[result.length] = f_string.substring(0, f_split_length);
            f_string = f_string.substring(f_split_length);
        }
        result[result.length] = f_string;
        return result;
    }
    return false;
}

function array_reverse (array, preserve_keys) {
    var arr_len = array.length, newkey = 0, tmp_arr = {}, key = '';
    preserve_keys = !!preserve_keys;
    
    for (key in array) {
        newkey = arr_len - key - 1;
        tmp_arr[preserve_keys ? key : newkey] = array[key];
    }

    return tmp_arr;
}

function strrev (string) {
    var ret = '', i = 0;

    string += '';
    for ( i = string.length-1; i >= 0; i-- ){
       ret += string.charAt(i);
    }

    return ret;
}
function substr (f_string, f_start, f_length) {
    f_string += '';

    if (f_start < 0) {
        f_start += f_string.length;
    }

    if (f_length == undefined) {
        f_length = f_string.length;
    } else if (f_length < 0){
        f_length += f_string.length;
    } else {
        f_length += f_start;
    }

    if (f_length < f_start) {
        f_length = f_start;
    }

    return f_string.substring(f_start, f_length);
}

///////////////////////////// AJAX //////////////////////////////////
function cartDel(username,session,id,trid){
	send_xmlhttprequest(cartDelResponse, 'GET', './includes/ajax.cartDel.php?username='+username+'&session='+session+'&item_id='+id+'&trid='+trid); 	
	return false;
}
function cartDelResponse(xmlhttp){
	if (xmlhttp.readyState == 4 || xmlhttp.readyState=="complete") 
	{
		var error = ''; 
		if (xmlhttp.responseXML != null && xmlhttp.responseXML.getElementsByTagName('result')[0] != null ){ 
			var tr = document.getElementById('kosik_tr_' + xmlhttp.responseXML.getElementsByTagName('id')[0].firstChild.data);
			tr.className = 'display_none'; 
		}
		else if (xmlhttp.responseXML != null && xmlhttp.responseXML.getElementsByTagName('error')[0] != null ){ 
			error = xmlhttp.responseXML.getElementsByTagName('error')[0]; 
			document.getElementById('kosik').innerHTML = '<h5 class="error">'+error.firstChild.data+'</h5><br/>'+document.getElementById('kosik').innerHTML;
		}
		else alert('Application error.');
	}	
}

function send_xmlhttprequest(state_change, method, url, content, headers) {
    var xmlhttp = (XMLHttpRequest ? new XMLHttpRequest : (ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : false));
    if (!xmlhttp) {
        return false;
    }
    xmlhttp.open(method, url);
    xmlhttp.onreadystatechange = function () {
        state_change(xmlhttp);
    };
    if (headers) {
        for (var key in headers) {
            xmlhttp.setRequestHeader(key, headers[key]);
        }
    }
    xmlhttp.send(content);
    return true;
}
