jQuery(document).ready(function() {
	
	jQuery('.antispam').each(function (index) {
		this.value = 999;
	});	

	var input = jQuery("#quicksearch");	
	input.val("rýchle vyhľadávanie");
	input.click(function() {		
		changeSearchValue(input);
	});

	input.focus(function() {		
		changeSearchValue(input);
	});
	input.blur(function() {
		if (input.val().length == 0) { 
			input.val("rýchle vyhľadávanie");
		}
	});
	initMenu();
	function changeSearchValue(e) {
		e.val((e.val() == "rýchle vyhľadávanie") ? "" : e.val());
	}
	if(typeof(initDatepickersJS)!="undefined"){
		initDatepickers();
	}
	if(typeof(initWatchdog)!="undefined"){
		initWatchdogForm();
	}
	if(typeof(initPcConfigurator)!="undefined"){
		initPcConfiguratorForm();
	}
	
	/* colorbox v detaile tovaru */
	jQuery("#previewPane img").click(function(e){		
		jQuery.fn.colorbox.init();		
		jQuery("#theImages a").colorbox({			
			rel: 'galleryImages',
			open: true,
			onClosed: function(){
				jQuery.fn.colorbox.remove();
			}
		});		
	});
	
	initRegistration();
	
	/* colorbox na celej stranke pri odkazoch s class gallery */
	if (typeof jQuery("a.gallery").colorbox == 'function') {
		jQuery("a.gallery").colorbox();
	}	
	
	if (typeof jQuery(".tooltip").tooltip == 'function') {
	
		jQuery(".tooltip").tooltip({
			bordercolor: '#eeeeee',
			bgcolor: '#ffffff',
			fontcolor: '#666666',
			fontsize: '10px',
			width: '100px',
			cursor: null
		});
	
	}
	if(!jQuery("#fixedDiv")){
		onPageLoad();
	}
	
	if (typeof $("#search").autocomplete == 'function') {
		jQuery("#search").autocomplete({
			source: "/search/autocomplete",
			minLength: 1,
			select: function(event, ui) { 
				var selectedObj = ui.item;
				$('#search').val(selectedObj.value);
				$('#iksearch').val(selectedObj.ik);
				$('#top_ban_search').submit();
			}
		});
	}
});


function initRegistration(){
	if(jQuery('#registrationType-company')!=null){
		jQuery('#registrationType-company').click(switchRegistrationType);
	}
	if(jQuery('#registrationType-person')!=null){
		jQuery('#registrationType-person').click(switchRegistrationType);
	}	
	if(jQuery('#registrationType-person')!=null && jQuery('#registrationType-company')!=null){
		switchRegistrationType();
	}
}

function switchRegistrationType(){
	if(jQuery('#registrationType-person').attr('checked')==true){			
		jQuery('#companyBoxTitle').html("Fakturačná adresa");
		jQuery('#addressDeliveryBoxTitle').html("Dodacia adresa");
		jQuery('#addressDeliveryBoxTitle').parent().removeAttr("onmouseover");		
		jQuery('#addressDeliveryBoxTitle').hover(function(){
			tooltip('Dodaciu adresu vyplňte iba v prípade, ak sa dodacia adresa odlišuje od fakturačnej a chcete, aby Vám tovar prichádzal na túto adresu.');
		});
		jQuery('#row-companyName').hide();
		jQuery('#row-ico').hide();
		jQuery('#row-dic').hide();
		jQuery('#row-icdph').hide();		
	}
	if(jQuery('#registrationType-company').attr('checked')==true){
		jQuery('#companyBoxTitle').html("Firemné údaje");
		jQuery('#addressDeliveryBoxTitle').html("Adresa prevádzky");
		jQuery('#addressDeliveryBoxTitle').parent().removeAttr("onmouseover");
		jQuery('#addressDeliveryBoxTitle').hover(function(){
			tooltip('Adresu prevádzky vyplňte iba v prípade, ak sa adresa prevádzky odlišuje od sídla Vašej firmy a chcete, aby Vám tovar prichádzal na túto prevádzku.');
		});
		jQuery('#row-companyName').show();
		jQuery('#row-ico').show();
		jQuery('#row-dic').show();
		jQuery('#row-icdph').show();				
	}
}

function redirectIpBox(element, url)
{	
	id = element.value;	

	if (id!='0') {
		window.location = '/redirect/product/id/'+id;
	}
}

function initMenu(){
	//jQuery('#eshopMenu').clickMenu();  
}

/* JS KATTREE */
function showKat(cat){
	jQuery('#cat_2_'+cat).toggle();
	var display = jQuery('#cat_2_'+cat).css("display");
	if(display=="block"){
		jQuery('#img_2_'+cat).attr("src","/images/list_minus.gif");
	}else{
		jQuery('#img_2_'+cat).attr("src","/images/list_plus.gif");
	}
}
function showReclamationRowDetail(row){
	jQuery('#detail_'+row).toggle();
	var display = jQuery('#detail_'+row).css("display");	
	if(display!="none"){
		jQuery('#img_'+row).attr("src","/images/list_minus.gif");
	}else{
		jQuery('#img_'+row).attr("src","/images/list_plus.gif");
	}
}

function showAllReclamationDetail() {
	var elements = jQuery("tr.link_ReclamationRowDetail");	
	elements.each(function(i){		
		showReclamationRowDetail(this.id);
	});	
}

function externalLinks() {  
	if (!document.getElementsByTagName) return;  
	var anchors = document.getElementsByTagName("a");  
	for (var i=0; i<anchors.length; i++) {  
		var anchor = anchors[i];  
		if (anchor.getAttribute("href") &&  
			anchor.getAttribute("rel") == "external") {  
			anchor.target = "_blank";  
		}
	}  
}  

jQuery.fn.wait = function(time, type) {
    time = time || 1000;
    type = type || "fx";
    return this.queue(type, function() {
        var self = this;
        setTimeout(function() {
        	jQuery(self).dequeue();
        }, time);
    });
};

/* LANG DETECT */
var isOldIE = false;
function onPageLoad()	{
   if (navigator.appName.indexOf('Microsoft Internet Explorer') > -1)  {
      ver = navigator.appVersion.substr(navigator.appVersion.indexOf('MSIE ')+5,3);
      if (ver < 7) {
         document.getElementById('fixedDiv').style.position = 'absolute';
         window.onscroll = reloadFixedDiv;
      }
   }
}
function reloadFixedDiv()	{
   var d= document.getElementById('fixedDiv');
   d.style.display = 'none';
   d.style.display = 'block';
}
/* END LANG DETECT */

window.onload = externalLinks;

function showMOCPrice() {
	jQuery(".main_price").each(function(i, element) {
		var displayed =  jQuery(element).text();
		jQuery(element).text($(element).attr("data-moc"));
		jQuery(element).attr("data-moc", displayed);
	});
	jQuery(".priceDescription").each(function(i, element){
		if (jQuery(element).text().indexOf("Bez DPH")>-1) {
			jQuery(element).text(jQuery(element).text().replace("Bez DPH","s DPH"));
		}
		else if (jQuery(element).text().indexOf("s DPH")>-1) {
			jQuery(element).text(jQuery(element).text().replace("s DPH","Bez DPH"));
		}
	});
	if (jQuery(".dphButton").length > 0){
		jQuery(".dphButton").attr('class','dphButton-active');
	}
	else if (jQuery(".dphButton-active").length > 0){
		jQuery(".dphButton-active").attr('class','dphButton');
	}
	//zmena textu v tooltipe
	var text = jQuery("#data_switchPriceVATInc").text();
	jQuery("#data_switchPriceVATInc").text(jQuery("#data_switchPrice").text());
	jQuery("#data_switchPrice").text(text);
}
