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");
		}
	});	
	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 na celej stranke pri odkazoch s class gallery */
	if (typeof jQuery("a.gallery").colorbox == 'function') {
		jQuery("a.gallery").colorbox();
	}	
	
	if (typeof jQuery('a.lightbox').lightBox == 'function') {
	
		jQuery('a.lightbox').lightBox({
			imageLoading: '/scripts/plugins/lightbox/images/lightbox-ico-loading.gif',
			imageBtnClose: '/scripts/plugins/lightbox/images/lightbox-btn-close.gif',
			imageBtnPrev: '/scripts/plugins/lightbox/images/lightbox-btn-prev.gif',
			imageBtnNext: '/scripts/plugins/lightbox/images/lightbox-btn-next.gif'
		});
	
	}
	//initMenu();
	
	if (typeof $("#search").autocomplete == 'function') {
		jQuery('#text-search').autocomplete({
			source: "/search/autocomplete",
			minLength: 3,
			disabled: jQuery("#typ-0:checked").length > 0 ? false : true,
			select: function(event, ui) { 
				var selectedObj = ui.item;
				$('#text-search').val(selectedObj.value);
				$('#ikSearch').val(selectedObj.ik);
				$('#searchForm').submit();
			}
		});	
	
		jQuery("#typ-1").change(function(){
			if (jQuery("#typ-1:checked").length>0) {
				jQuery('#text-search').autocomplete({ disabled: true });
			}
		});
		jQuery("#typ-0").change(function(){
			if (jQuery("#typ-0:checked").length>0) {
				jQuery('#text-search').autocomplete({ disabled: false });
			}
		});
	}
	
});

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);
    });
};

function potvrdenieVsetkychTerminovCheck(form) {
	jQuery('.row_date').val(form.datum_all.value);
	jQuery('.amount_input').each(function(i, element) {
		var row = jQuery(element).attr('id').replace('mnozstvo_','');
		jQuery(element).val(jQuery('#max_'+row).val());
	});
	return false;
}

function checkAmountToConfirm(input, max) {	
	var value = Math.abs(input.value);
	if (!value) {
		return false;		
	}
	
	input.value = value;	
	if (input.value > max) {
		alert ("Zadané množstvo nemôže byť väčšie ako objednané. Počet bude upravený na "+max);
		input.value = max;
		return false;
	}
	return true;
}

window.onload = externalLinks;
