/**
 *  BY LUKAS JEVICKY, 2009
 *  homepage: www.jevicky.cz [czech]
 *  depends on jQuery 1.3.x +
 */
  var DOMAIN_NAME = "";
  
   $(document).ready(function() {
		
	  $('.mp').maphilight({
	    fill: true,
	    fillColor: 'ffffff',
	    fillOpacity: 0.2,
	    stroke: true,
	    strokeColor: 'ffffff',
	    strokeOpacity: 1,
	    strokeWidth: 3,
	    fade: true,
	    alwaysOn: false
    });
	
    //$("#mp area[title]").tooltip();
	$("#sel_cat").treeview({
	  collapsed: true,
	  animated: "medium",
	  persist: "location"

	});
	
	$("a[rel=fb_img]").fancybox({
	  'transitionIn'		: 'none',
	  'transitionOut'		: 'none',
	  'titlePosition' 		: 'over',
	  'titleFormat'		: function(title, currentArray, currentIndex, currentOpts) {
	    return '<span id="fancybox-title-over">Image ' + (currentIndex + 1) + ' / ' + currentArray.length + (title.length ? ' &nbsp; ' + title : '') + '</span>';
	  }
	});
	
	$("#podminky").fancybox({
	  'titlePosition'		: 'inside',
	  'transitionIn'		: 'none',
	  'transitionOut'		: 'none',
	  'autoDimensions'	    : false,
	  'width'				: '75%',
	  'height'				: '75%'
	});
	
	$.featureList(
		$("#tabs li a"),
		$("#output li"), {
			start_item	:	1,
			transition_interval: 10000
		}
	);
	

/*
		$( "#search_text" ).autocomplete({
			source: "./autocomplete/search.php",
			minLength: 2,
			delay: 0,
			disabled: true
		});
	*/	
		  
  });
	
	function nav_sel_2_click() {
    requestData('../inc/ajx/search_deal.php','top_search_box');
  }
	
	function nav_sel_1_click() {
    requestData('../inc/ajx/search_company.php','top_search_box');
  }
  
  function add_this_cat(cat) {
    requestData('../inc/ajx/add_cat.php?vvv='+encodeURIComponent(cat),'vybrane_kat');
  }
  
  function refresh_cat() {
    requestData('../inc/ajx/add_cat.php','vybrane_kat');
  }
  
  function refresh_photo() {
    requestData('../inc/ajx/sel_photo.php','vybrane_photo');
  }
  
  function del_this_cat(cat) {
    requestData('../inc/ajx/add_cat.php?ddd='+encodeURIComponent(cat),'vybrane_kat');
  }
  
  function del_this_photo(photo) {
    requestData('../inc/ajx/sel_photo.php?ddd='+encodeURIComponent(photo),'vybrane_photo');
  }
  
  function requestSelObec(id_okres) {
    //if(id_zeme != '0') {
      requestVotes(DOMAIN_NAME+'/inc/ajx/sel_obec.php?vvv='+id_okres, 'stat_com_obec');
    //}
  }

  function requestVotes(getPage, divName) {
    $("#"+divName).load(getPage); 
    return false;
  }


  function requestData(getPage, divName) {
    $("#"+divName).load(getPage, function() {
      $("#"+divName).show();
    } );
    return false;
  }

  function odeslat() {
    var jmeno = encodeURIComponent(document.getElementById('jmeno').value);
	var prijmeni = encodeURIComponent(document.getElementById('prijmeni').value);
    var email = document.getElementById('email').value;
	var telefon = document.getElementById('telefon').value;
    var zprava = encodeURIComponent(document.getElementById('zprava').value);
    // test validace emailu
    var vyraz = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
    if(vyraz.test(email) == false) {
      alert("Chybný tvar e-mailu. Prosím, zkontrolujte e-mailovou adresu a zkuste zprávu odeslat znovu.");
    } else if(trim(document.getElementById('jmeno').value) == '') {
	  alert("Prosíme o vyplnění Vašeho jména");
	} else if(trim(document.getElementById('prijmeni').value) == '') {
	  alert("Prosíme o vyplnění Vašeho přijmení");
	} else if(trim(document.getElementById('zprava').value) == '') {
	  alert("Prosíme o vyplnění Vašeho dotazu");
	} else {
      // zpracovani emailu z formulare, nasledne zobrazeni hlasky
      $("#zpracujZpravu").load(DOMAIN_NAME+"/inc/ajx/send_email.php?jmeno="+jmeno+"&prijmeni="+prijmeni+"&telefon="+telefon+"&email="+email+"&zprava="+zprava, function() {
      //  alert("Váš e-mail byl uložen. Děkujeme a přejeme příjemný den.");
      });
    }

  }
  
  function odeslat_msg() {
    var prijemce = document.getElementById('company_contact').value;
	var frm_ic = document.getElementById('frm_ic').value;
	var jmeno = encodeURIComponent(document.getElementById('sender_name').value);
    var email = document.getElementById('sender_email').value;
	var zprava = encodeURIComponent(document.getElementById('contact_msg').value);
    // test validace emailu
    var vyraz = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
    if(vyraz.test(email) == false) {
      alert("Chybný tvar e-mailu. Prosím, zkontrolujte e-mailovou adresu a zkuste zprávu odeslat znovu.");
    } else if(trim(document.getElementById('sender_name').value) == '') {
	  alert("Prosíme o vyplnění Vašeho jména");
	} else if(trim(document.getElementById('sender_email').value) == '') {
	  alert("Prosíme o vyplnění Vašeho emailu");
	} else if(trim(document.getElementById('contact_msg').value) == '') {
	  alert("Prosíme o vyplnění Vašeho dotazu / poptávky");
	} else {
      $("#zpracujZpravu").load(DOMAIN_NAME+"/inc/ajx/send_msg.php?p="+prijemce+"&o="+jmeno+"&e="+email+"&z="+zprava+"&i="+frm_ic, function() {});
    }

  }
  
  function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
  }
  
  function ltrim(stringToTrim) {
	return stringToTrim.replace(/^\s+/,"");
  }
  
  function rtrim(stringToTrim) {
	return stringToTrim.replace(/\s+$/,"");
  }
  
  function showLoading() {
    centerLoading('loading','220','100');
    $("#loading").fadeTo(0, 1000, function() {
    $("#loading")
      .css({display:"block"})
      .css({visibility:"visible"})
      .css({opacity:"1"});
    });
  }

  function hideLoading() {
    $("#loading").fadeTo(1000, 0, function() {
      $("#loading").css({display:"none"});
    });
  };

  function centerLoading(nameOfPop,W,H) {

    var windowSize = getPageDimensions();
    var window_width  = windowSize[0];
    var window_height = windowSize[1];

    var scrollY = 0;

    if ( document.documentElement && document.documentElement.scrollTop ) {
      scrollY = document.documentElement.scrollTop;
    } else if ( document.body && document.body.scrollTop ) {
      scrollY = document.body.scrollTop;
    } else if ( window.pageYOffset ) {
      scrollY = window.pageYOffset;
    } else if ( window.scrollY ) {
      scrollY = window.scrollY;
    }

    var setX = ( window_width  - W  ) / 2;
    var setY = ( window_height - H ) / 2;

    setX = ( setX < 0 ) ? 0 : setX;
    setY = ( setY < 0 ) ? 0 : setY;

    document.getElementById(nameOfPop).style.top=setY+'px';
    document.getElementById(nameOfPop).style.left=setX+'px';
    document.getElementById(nameOfPop).style.display='block';
    document.getElementById(nameOfPop).style.position='fixed';
  }

  function getPageDimensions() {
    var xScroll, yScroll;

    if (window.innerHeight && window.scrollMaxY) {
      xScroll = document.body.scrollWidth;
      yScroll = window.innerHeight + window.scrollMaxY;
    } else if (document.body.scrollHeight > document.body.offsetHeight){
      xScroll = document.body.scrollWidth;
      yScroll = document.body.scrollHeight;
    } else {
      xScroll = document.body.offsetWidth;
      yScroll = document.body.offsetHeight;
    }

    var windowWidth, windowHeight;
    if (self.innerHeight) {
      windowWidth = self.outerWidth;
      windowHeight = self.innerHeight;
    } else if (document.documentElement && document.documentElement.clientHeight) {
      windowWidth = document.documentElement.clientWidth;
      windowHeight = document.documentElement.clientHeight;
    } else if (document.body) {
      windowWidth = document.body.clientWidth;
      windowHeight = document.body.clientHeight;
    }

    if(yScroll < windowHeight) {
      pageHeight = windowHeight;
    } else {
      pageHeight = yScroll;
    }

    if(xScroll < windowWidth) {
      pageWidth = windowWidth;
    } else {
      pageWidth = xScroll;
    }
    arrayPageSize = new Array(windowWidth,windowHeight,pageWidth,pageHeight)
    return arrayPageSize;
  }
