function reference(reference, new_window)
{
  if (new_window)
    window.open(reference, '');
  else
    parent.location.href = reference;
}

function infoAlert(info1, info2)
{
  alert(info1+"\n"+info2);
}

function showDelete(id)
{
  var newSS = document.styleSheets[1];

  document.getElementById('delete_file'+id).value = '1';
  createRule(newSS, '#span_file'+id, 'display: inline;');
}

function advertisementClick(reference, id)
{
  var ac = document.getElementById('ac');
  ac.innerHTML = '<img style="display: none;" src="/col_advertisement_click.php?id=' + id + '" />';

  window.open(reference);
}

function dialog(what, reference) 
{
  if (confirm("Opravdu chcete "+what+"?") == true)
    window.location = reference; 
  else
    return 0;
}

function blankDialog(what, reference) 
{
  if (confirm(what) == true)
    window.location = reference; 
  else
    return 0;
}

function blinkIt()
{
 if (!document.all) return;
 else
 {
   for(i=0;i<document.all.tags('blink').length;i++)
   {
      s=document.all.tags('blink')[i];
      s.style.visibility=(s.style.visibility=='visible')?'hidden':'visible';
   }
 }
}

function selectTransportCountry()
{
  var newSS = document.styleSheets[1];
  
  for(i=1;i<=30;i++)
  {
    createRule(newSS, '#transport_country_'+i, 'display: none;');
  }
  
  var select  = document.getElementById('country');
  var country = document.getElementById('country').options[select.selectedIndex].value;
  createRule(newSS, '#transport_country_'+country, 'display: block;');
}

function seoDescriptionHeight(id)
{
  var newSS = document.styleSheets[1];
  
  var ver = navigator.appVersion;
  if ((ver.indexOf("MSIE 6") != -1) || (ver.indexOf("MSIE 7") != -1))
    var height = description.offsetHeight;
  else
  {
    var ide = document.getElementById(id);
    if (ide)
      var height = ide.offsetHeight;
  }
  //height = height + 15;
  createRule(newSS, '#indescription', 'height: '+height+'px;');
}

function windowWidth()
{
	var w = 0;

	//IE
	if(!window.innerWidth)
	{
		//strict mode
		if(!(document.documentElement.clientWidth == 0))
			w = document.documentElement.clientWidth+18;
		//quirks mode
		else
			w = document.body.clientWidth+18;
	}
	//w3c
	else
		w = window.innerWidth;
  
  return w;
}

function linkCheck(id)
{
  var checkbox = document.getElementById('link_check_'+id);
  
  if (checkbox.checked == true)
    checkbox.checked = false;
  else
    checkbox.checked = true;
}

function uncheckAll(theElement)
{
  var theForm = theElement.form, z = 0;
	for(z=0; z<theForm.length; z++)
	{
    if(theForm[z].type == 'checkbox')
      theForm[z].checked = theElement.checked;
  }
}

function numbersOnly(myfield, e, dec)
{
  var key;
  var keychar;
  
  if (window.event)
     key = window.event.keyCode;
  else if (e)
     key = e.which;
  else
     return true;
  keychar = String.fromCharCode(key);
  
  // control keys
  if ((key==null) || (key==0) || (key==8) || 
      (key==9) || (key==13) || (key==27) )
     return true;
  
  // numbers
  else if ((("0123456789").indexOf(keychar) > -1))
     return true;
  
  // decimal point jump
  else if (dec && (keychar == "."))
     {
     myfield.form.elements[dec].focus();
     return false;
     }
  else
     return false;
}

function changeBookmark(id)
{
  var newSS = document.styleSheets[1];

  if (id==1)
  {
    createRule(newSS, '#bookmark_on1', 'display: block;');
    createRule(newSS, '#bookmark_off1', 'display: none;');
    createRule(newSS, '#bookmark_text1', 'display: block;');

    createRule(newSS, '#bookmark_on2', 'display: none;');
    createRule(newSS, '#bookmark_off2', 'display: block;');
    createRule(newSS, '#bookmark_text2', 'display: none;');
    createRule(newSS, '#bookmark_on3', 'display: none;');
    createRule(newSS, '#bookmark_off3', 'display: block;');
    createRule(newSS, '#bookmark_text3', 'display: none;');
    createRule(newSS, '#bookmark_on4', 'display: none;');
    createRule(newSS, '#bookmark_off4', 'display: block;');
    createRule(newSS, '#bookmark_text4', 'display: none;');
    createRule(newSS, '#bookmark_on5', 'display: none;');
    createRule(newSS, '#bookmark_off5', 'display: block;');
    createRule(newSS, '#bookmark_text5', 'display: none;');
  }
  else if (id==2)
  {
    createRule(newSS, '#bookmark_on2', 'display: block;');
    createRule(newSS, '#bookmark_off2', 'display: none;');
    createRule(newSS, '#bookmark_text2', 'display: block;');

    createRule(newSS, '#bookmark_on1', 'display: none;');
    createRule(newSS, '#bookmark_off1', 'display: block;');
    createRule(newSS, '#bookmark_text1', 'display: none;');
    createRule(newSS, '#bookmark_on3', 'display: none;');
    createRule(newSS, '#bookmark_off3', 'display: block;');
    createRule(newSS, '#bookmark_text3', 'display: none;');
    createRule(newSS, '#bookmark_on4', 'display: none;');
    createRule(newSS, '#bookmark_off4', 'display: block;');
    createRule(newSS, '#bookmark_text4', 'display: none;');
    createRule(newSS, '#bookmark_on5', 'display: none;');
    createRule(newSS, '#bookmark_off5', 'display: block;');
    createRule(newSS, '#bookmark_text5', 'display: none;');
  }  
  else if (id==3)
  {
    createRule(newSS, '#bookmark_on3', 'display: block;');
    createRule(newSS, '#bookmark_off3', 'display: none;');
    createRule(newSS, '#bookmark_text3', 'display: block;');

    createRule(newSS, '#bookmark_on1', 'display: none;');
    createRule(newSS, '#bookmark_off1', 'display: block;');
    createRule(newSS, '#bookmark_text1', 'display: none;');
    createRule(newSS, '#bookmark_on2', 'display: none;');
    createRule(newSS, '#bookmark_off2', 'display: block;');
    createRule(newSS, '#bookmark_text2', 'display: none;');
    createRule(newSS, '#bookmark_on4', 'display: none;');
    createRule(newSS, '#bookmark_off4', 'display: block;');
    createRule(newSS, '#bookmark_text4', 'display: none;');
    createRule(newSS, '#bookmark_on5', 'display: none;');
    createRule(newSS, '#bookmark_off5', 'display: block;');
    createRule(newSS, '#bookmark_text5', 'display: none;');
  }  
  else if (id==4)
  {
    createRule(newSS, '#bookmark_on4', 'display: block;');
    createRule(newSS, '#bookmark_off4', 'display: none;');
    createRule(newSS, '#bookmark_text4', 'display: block;');

    createRule(newSS, '#bookmark_on1', 'display: none;');
    createRule(newSS, '#bookmark_off1', 'display: block;');
    createRule(newSS, '#bookmark_text1', 'display: none;');
    createRule(newSS, '#bookmark_on2', 'display: none;');
    createRule(newSS, '#bookmark_off2', 'display: block;');
    createRule(newSS, '#bookmark_text2', 'display: none;');
    createRule(newSS, '#bookmark_on3', 'display: none;');
    createRule(newSS, '#bookmark_off3', 'display: block;');
    createRule(newSS, '#bookmark_text3', 'display: none;');
    createRule(newSS, '#bookmark_on5', 'display: none;');
    createRule(newSS, '#bookmark_off5', 'display: block;');
    createRule(newSS, '#bookmark_text5', 'display: none;');
  }  
  else if (id==5)
  {
    createRule(newSS, '#bookmark_on5', 'display: block;');
    createRule(newSS, '#bookmark_off5', 'display: none;');
    createRule(newSS, '#bookmark_text5', 'display: block;');

    createRule(newSS, '#bookmark_on1', 'display: none;');
    createRule(newSS, '#bookmark_off1', 'display: block;');
    createRule(newSS, '#bookmark_text1', 'display: none;');
    createRule(newSS, '#bookmark_on2', 'display: none;');
    createRule(newSS, '#bookmark_off2', 'display: block;');
    createRule(newSS, '#bookmark_text2', 'display: none;');
    createRule(newSS, '#bookmark_on3', 'display: none;');
    createRule(newSS, '#bookmark_off3', 'display: block;');
    createRule(newSS, '#bookmark_text3', 'display: none;');
    createRule(newSS, '#bookmark_on4', 'display: none;');
    createRule(newSS, '#bookmark_off4', 'display: block;');
    createRule(newSS, '#bookmark_text4', 'display: none;');
  }  
}

function stockChange(id, radio)
{
  var newSS = document.styleSheets[1];
  var stock_actual_1  = readCookie("stock_actual_1");
  var stock_actual_2  = readCookie("stock_actual_2");
  var stock_actual_3  = readCookie("stock_actual_3");
  var stock_actual_4  = readCookie("stock_actual_4");
  var stock_actual_5  = readCookie("stock_actual_5");
  
  if (!stock_actual_1) stock_actual_1 = '';
  if (!stock_actual_2) stock_actual_2 = '';
  if (!stock_actual_3) stock_actual_3 = '';
  if (!stock_actual_4) stock_actual_4 = '';
  if (!stock_actual_5) stock_actual_5 = '';

  if (id)
  {
    if ((radio) && (radio<=100))
    {
      var stock_actual = stock_actual_1;
      createRule(newSS, '.connect_'+stock_actual, 'display: none;');
      var connect = '-'+id;

      createRule(newSS, '.connect_'+connect, 'display: block;');
      
      createCookie('stock_actual_1', connect, 365);
    }
    else
    {
      if (stock_actual_5)
        var stock_actual = stock_actual_1+stock_actual_2+stock_actual_3+stock_actual_4+stock_actual_5;
      else if (stock_actual_4)
        var stock_actual = stock_actual_1+stock_actual_2+stock_actual_3+stock_actual_4;
      else if (stock_actual_3)
        var stock_actual = stock_actual_1+stock_actual_2+stock_actual_3;
      else if (stock_actual_2)
        var stock_actual = stock_actual_1+stock_actual_2;    
      else if (stock_actual_1)
        var stock_actual = stock_actual_1;
      
      createRule(newSS, '.connect_'+stock_actual, 'display: none;');

      if (radio>100)
        var connect_this = document.getElementById('radio_'+radio).value;
      else
        var connect_this = document.getElementById('select_'+id).value;
      connect_this = '-'+connect_this;
      
      if (id==1)
        var connect = connect_this+stock_actual_2+stock_actual_3+stock_actual_4+stock_actual_5;
      if (id==2)
        var connect = stock_actual_1+connect_this+stock_actual_3+stock_actual_4+stock_actual_5;
      if (id==3)
        var connect = stock_actual_1+stock_actual_2+connect_this+stock_actual_4+stock_actual_5;
      if (id==4)
        var connect = stock_actual_1+stock_actual_2+stock_actual_3+connect_this+stock_actual_5;
      if (id==5)
        var connect = stock_actual_1+stock_actual_2+stock_actual_3+stock_actual_4+connect_this;

      createRule(newSS, '.connect_'+connect, 'display: block;');

      createCookie('stock_actual_'+id, connect_this, 365);
    }
  }
  else
  {
    if (stock_actual_5)
      createRule(newSS, '.connect_'+stock_actual_1+stock_actual_2+stock_actual_3+stock_actual_4+stock_actual_5, 'display: block;');
    else if (stock_actual_4)
      createRule(newSS, '.connect_'+stock_actual_1+stock_actual_2+stock_actual_3+stock_actual_4, 'display: block;');
    else if (stock_actual_3)
      createRule(newSS, '.connect_'+stock_actual_1+stock_actual_2+stock_actual_3, 'display: block;');
    else if (stock_actual_2)
      createRule(newSS, '.connect_'+stock_actual_1+stock_actual_2, 'display: block;');    
    else if (stock_actual_1)
      createRule(newSS, '.connect_'+stock_actual_1, 'display: block;');
  }
}

function stockHide(connect)
{
  var newSS = document.styleSheets[1];  
  createRule(newSS, '.connect_'+connect, 'display: none;');
}

function stockInit(id, connect)
{
  createCookie('stock_actual_'+id, connect, 365);
}

function stockReset()
{
  createCookie('stock_actual_1', '', 365);
  createCookie('stock_actual_2', '', 365);
  createCookie('stock_actual_3', '', 365);
  createCookie('stock_actual_4', '', 365);
  createCookie('stock_actual_5', '', 365);
}

function show_variant(id)
{
  var newSS = document.styleSheets[1];
  var check = document.getElementById('check_'+id).checked;

  if (check == true)
    createRule(newSS, '.var_'+id, 'display: inline;');
  else
    createRule(newSS, '.var_'+id, 'display: none;');
}

function hide_variant(id)
{
  var newSS = document.styleSheets[1];
  
  createRule(newSS, '.var_'+id, 'display: none;');
}

function createRule(stylesheet, selektor, deklarace)
{
  if (stylesheet.cssRules)
  {
    var rules = stylesheet.cssRules;
  } else if (stylesheet.rules)
  {
    var rules = stylesheet.rules;
  }
  
  var index = rules.length;
  
  if (stylesheet.insertRule)
  {
    var ruleText = selektor+'{'+deklarace+'}';
    stylesheet.insertRule(ruleText, index);
  } else if (stylesheet.addRule)
  {
    stylesheet.addRule(selektor, deklarace, index);
  }
}
	
function newRule(id)
{
  var newSS = document.styleSheets[1];
  
  if (id == "10")
  {
    createRule(newSS, '.deliver', 'display: none;');
    createCookie("deliver_cookie", id, 365);
  }
  if (id == "11")
  {
    createRule(newSS, '.deliver', 'display: block;');
    createCookie("deliver_cookie", id, 365);
  }    
  if (id == "20")
  {
    createRule(newSS, '.invoice', 'display: none;');  
    createCookie("invoice_cookie", id, 365);
  } 
  if (id == "21")
  {
    createRule(newSS, '.invoice', 'display: block;');  
    createCookie("invoice_cookie", id, 365);
  } 
}

function navigator_order()
{
  var aaa = document.getElementById('order').value;
  location.href=(aaa);
}

function navigator_products()
{
  var aaa = document.getElementById('products').value;
  location.href=(aaa);
}

function navigator_glproducer()
{
  var aaa = document.getElementById('glproducer').value;
  location.href=(aaa);
}

function navigator_dir()
{
  var aaa = document.getElementById('dir').value;
  location.href=(aaa);
}

function navigator_price()
{
  var aaa = document.getElementById('price').value;
  location.href=(aaa);
}

function navigator_medium()
{
  var aaa = document.getElementById('medium').value;
  location.href=(aaa);
}

function navigator_duration()
{
  var aaa = document.getElementById('duration').value;
  location.href=(aaa);
}

function navigatorToID(id)
{
  var aaa = document.getElementById(id).value;
  location.href=(aaa);
}

function locationToID(where, direct)
{
  if (direct)
    location.href=(where);
  else
  {
    var aaa = document.getElementById(where).value;
    location.href=(aaa);
  }
}

function createCookie(name,value,days) {
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  }
  else expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
}

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 openNewWindow(reference)
{
  window.open(reference, '');
}

function NetShops()
{
  url = 'http://www.netshops.cz/';
  window.open(url, '');
}

function compareWindow(reference)
{
  iMyWidth = (window.screen.width/2) - (450 + 10);
  iMyHeight = (window.screen.height/2) - (200 + 25);
  Fokus = window.open(reference, "","width=900, height=400, resizable=yes, left=" + iMyWidth + ", top=" + iMyHeight + ", screenX=" + iMyWidth + ", screenY=" + iMyHeight + ", scrollbars=yes");
  Fokus.focus();
}

function openPhoto(server, id)
{
  iMyWidth = (window.screen.width/2) - (400 + 10)
  iMyHeight = (window.screen.height/2) - (300 + 25)
  Fokus = window.open(server+"/photo.php?id="+id, "","height=600,width=800,resizable=yes,left=" + iMyWidth + ",top=" + iMyHeight + ",screenX=" + iMyWidth + ",screenY=" + iMyHeight + ",scrollbars=yes");
  Fokus.focus()
}

function eraseText(what)
{
  string = document.getElementById(what).value;
  first = string.substr(0,1);
  sixth = string.substr(5,1);
  if ((first==' ') || (first=='U') || (sixth=='X'))
    document.getElementById(what).value = '';
}

function hideCetelemButton()
{
  var newSS = document.styleSheets[1];
  createRule(newSS, '#send_request', 'display: none;');
}

function checkBox(id, what)
{
  var checkbox = document.getElementById(what+id);
  
  if (checkbox.checked == true)
    checkbox.checked = false;
  else
    checkbox.checked = true;
}

rating = {
	
	imgOff : "/_img/star_off.png",
	imgOn : "/_img/star_on.png",
	
	highlight : function (n) {
		n = ((n >= 0) && (n <= 5)) ? n : 0;
		for (var i = 1; i <= 5; i++) {
			var elm = document.getElementById("rating" + i);
			if (elm) {
				elm.src = (n <= i-1) ? rating.imgOff : rating.imgOn;
			}
		}
	}
}

function numberFormat_1(text)
{
	//Reverse string
	//Loop through adding every character
	//If index = 3, add a comma
	//Reverse string
	
  var decimal = text.indexOf(".", text);

	if (decimal==false)
	{
    text = text.split("").reverse().join("");
  	var output = "";
  	var j=0;
  	for(var i = 0;i<text.length;i++) {
  		output += text.charAt(i);
  		j++;
  		if(j==3 && i != text.length-1) {
  			output += " ";
  			j=0;
  		}
  	}
  	output = output.split("").reverse().join("");
  	return output;
	}
	else
	{
    text = text.split("").reverse().join("");
  	var output = "";
  	var j=0;
  	for(var i = 0;i<text.length;i++) {
  		output += text.charAt(i);
  		j++;
  		if(j==4 && i != text.length-1) {
  			output += "";
  			j=0;
  		}
  	}
  	output = output.split("").reverse().join("");
  	return output;
  }
}

function checkProducer(id)
{
  var checkbox = document.getElementById('f_producer_'+id);
  checkbox.checked = true;
  document.getElementById('filter_producers').submit();
}

var deliver_cookie = readCookie("deliver_cookie");
newRule(deliver_cookie);

var invoice_cookie = readCookie("invoice_cookie");
newRule(invoice_cookie);

/* ---------------------------------------- Autocomplete ---------------------------------------- */

$(function() {
	$("#autocomplete").autocomplete('/autocomplete/', {
    cacheLength: 1,
    matchSubset: false,
    selectFirst: false,
		max: 100,
    width: 300,
    scroll: false,
    dataType: "json",
		parse: function(data) {
			return $.map(data, function(row) {
				return {
					data: row,
					value: row.name,
					result: row.name
				}
			});
		},
		formatItem: function(item) {
      return item.name;
		}
	}).result(function(e, item) {
		location.href = item.url;
	});
});

/* ------------------------------------- Screenshot Preview ------------------------------------- */

this.screenshotPreview = function(){	
	/* CONFIG */
		
		xOffset = 10;
		yOffset = 30;
		
		// these 2 variable determine popup's distance from the cursor
		// you might want to adjust to get the right result
		
	/* END CONFIG */
	$("a.screenshot").hover(function(e){
		this.t = this.title;
		this.title = "";	
		var c = (this.t != "") ? "<br/>" + this.t : "";
		$("body").append("<p id='screenshot'><img src='"+ this.rel +"' alt='url preview' />"+ c +"</p>");								 
		$("#screenshot")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px")
			.fadeIn("fast");						
    },
	function(){
		this.title = this.t;	
		$("#screenshot").remove();
    });	
	$("a.screenshot").mousemove(function(e){
		$("#screenshot")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px");
	});			
};


// starting the script on page load
$(document).ready(function(){
	screenshotPreview();
});
