
$(document).ready(function() {

		// -------------------------------------------------- NASTAVÍME MENU S KATEGORIEMI -------------------------------------------------- 
	  
	  if(document.getElementById('categoryMenu') != null) {
	  
			// prolezeme všechny kategorie a ty, které nemají pod kategorii, tak odstraníme znaménko
			ids = new Array();
			spanIds = "";		
				
			spanIds = $('#span_ids').attr('class');

			if(spanIds != '') {
			
				ids = spanIds.split('_');
			
				for(i = 0; i < ids.length; i++) {
			
					if(document.getElementById('ul_category_' + ids[i]) == null) { // pokud NEexistuje již žádná další podkategorie, tak uděláme s tlačítka odkaz
	
						$('#sign_' + ids[i]).attr('src', $('#sign_blank').attr('src'));
					}
				}
			}
		
	  	
		 	// načteme si cookie a vložíme mínus do všech odkrytých kategorií
		 	cookieContent = getCookie('katybaby_obchod_category_menu');
	 		
		 	if(cookieContent != '') {

			 	items = new Array();
			 	items = cookieContent.split(':');

	 			for(i = 0; i < items.length; i++) {
	 	
	 				parts = new Array();
		 			parts = items[i].split('#');
		 		
		 			if(document.getElementById('ul_category_' + parts[0]) != null) { // pokud existují nějaké podkategorie
					
						$('#sign_' + parts[0]).attr('src', $('#sign_minus').attr('src'));
					}
			 	}
			}
		}
	
});

var web = "http://www.katybaby.cz/";

jQuery(function($){
	
	$('#header .container').click(function() {
		window.location.href = web;
	});
		
});

// =================================================================

// skrývání / odkrývání prvků
function unHideCategory(id, lft, rgt, url) {
	
	var akce;
	
	if($('#ul_category_' + id).css('display') == 'none') { // budeme odkrývat
	
		akce = 1;
	
		$('#ul_category_' + id).show('normal');
	
		// změníme ikonu na minus
		$('#sign_' + id).attr('src', $('#sign_minus').attr('src'));
		
	} else { // budeme skrývat
	
		akce = 2;
	
		$('#ul_category_' + id).hide('normal');
	
		// změníme ikonu na minus
		$('#sign_' + id).attr('src', $('#sign_plus').attr('src'));
	}
	
	value = getCookie('katybaby_obchod_category_menu');

	if(akce == 1) { // přidáváme

		value = addToCookie(value, id, lft, rgt);
		
	} else if(akce == 2) { // odebíráme
	
		value = removeFromCookie(value, id, lft, rgt);	
	}
	
	// uložíme si cookie
	setCookie('katybaby_obchod_category_menu', value, 365, '/'); 
	
	if(document.getElementById('ul_category_' + id) == null) { // pokud NEexistuje již žádná další podkategorie, tak uděláme s tlačítka odkaz
	
		document.location.href = url;
		
	} else {
	
		return false;
	}
}

// =================================================================

// ukládání do cookie to, kde jsme právě klikli
function linkToCategory(id, lft, rgt, url) {
	
	value = getCookie('katybaby_obchod_category_menu');

	value = addToCookie(value, id, lft, rgt);
	
	// uložíme si cookie
	setCookie('katybaby_obchod_category_menu', value, 365, '/'); 
	
	return true;
}

// =================================================================

// redirect na stránku
function redirect(url) {

	document.location.href = url;
}

// =================================================================

// klasická confirm
function confirm(question) {	   
	
	return window.confirm(question);
}

// =================================================================

function setCookie(key, value, days, path) {

	value = encodeURIComponent(value); // hodnota value by měla být minimálně escapována pomocí funkce escape()
	value +='; path='+path;

	// expirace
	var date = new Date();
	date.setTime(date.getTime() + days * 24 * 60 * 60 * 1000);
	value+='; expires=' + date.toGMTString();

	document.cookie = key + '=' + value;
}
							
// ================================================================================================================

function isArray(input) {
    
	return typeof(input)=='object'&&(input instanceof Array);
}

// ================================================================================================================

function inArray(item, array) {

	if(!isArray(array)) { // nejedná se o pole, takže v něm prvek nemůže být

		return false;
	
	} else {
	 
		for(var i = 0; i < array.length; i++) {
		
			if(array[i] == item) {
			
				return true;
			}
		}
	}
	
	return false;
}

// ================================================================================================================

function getCookie(name) {
	
	// připravíme si řetězec pro cookie
	cookies = document.cookie;
	pos = cookies.indexOf(name + "=");
	
	if(pos != -1) { // pokud jsme něco našli
		
		// ořežeme si pouze údaje, které nás zajímají
		start = pos + (name.length+1);
		stop = cookies.indexOf(";", start);
		
		if(stop == -1) {
			stop = cookies.length;
		}
		
		value = cookies.substring(start, stop);
		value = unescape(value);	
	
	} else { // cookie ještě neexistuje
	
		value = "";
	}
	
	return value;
}

// ================================================================================================================

function addToCookie(cookie, id, lft, rgt) {
	
	isInArray = false; // logická hodnota říkající zda-li prvek již je v poli

	if(cookie != "") {
	
		items = new Array();
		pom = new Array();
	
		items = cookie.split(':');
	
		for(i = 0; i < items.length; i++) {
		
			pom = items[i].split('#');
		
			if(pom[0] == id) {
		
				isInArray = true;
				break;
			} 
		}
	}
	
	if(!isInArray) { // prvek ještě není v poli
		
		if(cookie != "") {
			cookie += ":" + id + "#" + lft + "#" + rgt;
		} else {
			cookie = id + "#" + lft + "#" + rgt;
		}
	}
	
	return cookie;
}

// ================================================================================================================

function removeFromCookie(cookie, id, lft, rgt) {
	
	isInArray = false; // logická hodnota říkající zda-li prvek již je v poli

	if(cookie != "") {
		
		newArray = new Array();		
		items = new Array();
		pom = new Array();
		
		j = 0;
		
		items = cookie.split(':');
		
		for(i = 0; i < items.length; i++) {
			
			pom = items[i].split('#');
			
			if(!(pom[1] >= lft && pom[2] <= rgt)) { // jedná se o prvek který nění v "ně"
				
				newArray[j] = items[i];
				j++;
			}
		}
	}
	
	return newArray.join(':');
}

// ================================================================================================================

function cookie2array(value) {
	
	if(value != "") { // pokud v cookie již něco je
		
		var newArray = new Array();
		
		var items = new Array();
		
		items = value.split(':');
		
		j = 0;
		
		for(i = 0; i < items.length; i++) {
			
			pom = new Array();
			pom = items[i].split('#');
			
			if(isArray(pom)) {
				
				newArray[j] = pom;
			}
		}
		
		return newArray;
	}
	
	return false;
}

// ================================================================================================================

function openWindow(url, resizable, scroll, width, height) {
    
	var msg = window.open(url, "popu", 'resizable=' + resizable + ',top=10,left=10,width=' + width + ',height=' + height + ',scrollbars=' + scroll + ',status=no,toolbar=no,menubar=no');
}

// ================================================================================================================ 

function sameAddress() {

	$('#profile_delivery_subject').val($('#profile_invoice_subject').val());
	$('#profile_delivery_address_street').val($('#profile_invoice_address_street').val());
	$('#profile_delivery_address_city').val($('#profile_invoice_address_city').val());
	$('#profile_delivery_address_zip').val($('#profile_invoice_address_zip').val());

	document.getElementById('profile_delivery_address_country').selectedIndex = document.getElementById('profile_invoice_address_country').selectedIndex;
}

// ================================================================================================================ 

function checkCondition(error) {
				
	if(!$('#condition').attr('checked')) {
				
		alert(error);
			
		return false;
	}
			
	return true;
}

// ================================================================================================================ 

function checkAddToBasket(id, error) {
    
	re = /^[0-9]+$/;
      
  if($('#amount_' + id).val().search(re) != 0) {
  
  	alert(error);
    document.getElementById('amount_' + id).focus();
    return false;
  }
			
	return true;
}

// ================================================================================================================ 

function unHideText(id, text1, text2) {
    
	if($('#a_sub_note').html() == text1) { // odkrýváme
		
		$('#sub_note').slideDown("slow");
		$('#a_sub_note').html(text2)
	
	} else { // skrýváme
		
		$('#sub_note').slideUp("slow");
		$('#a_sub_note').html(text1)	
	}
	
	return false;
}

// ================================================================================================================ 

function getPayment(id) {

	$.ajax({ 
					method: 		'post', 
      		url: 				'/index.php/order/getAjaxPayment', 
      		data: 			'id=' + id,
      		dataType:		'json',
      						  
          beforeSend: function() {

            $('#indicator_payment').show();
	        },
  	                    
    	    complete: 	function() {
      	          	
        	  $('#indicator_payment').hide();
          },  
		  								
		  		error: 			function() {
		  							
		  			alert('Došlo k chybě při komunikaci se serverem.');
			  	},
			  							
			  	success: 		function(json) {
						  				
						// zadisablujeme staré záznamy
						pom = $('#ids').val();
						
						if(pom != '') {
						
							ids = new Array();
							ids = pom.split(':');
							
							for(i = 0; i < ids.length; i++) {
								
								$('#payment_' + ids[i]).attr('disabled', 'disabled');
								$('#payment_' + ids[i]).attr('checked', false);
							}
						}
										
						if(json != 'NULL') {
	
							isChecked = false;
	
							for(var property in json) {                       					
								
								$('#payment_' + json[property]).attr('disabled', '');
								
								if(!isChecked) { // zacheckujeme prvek			
								
									$('#payment_' + json[property]).attr('checked', true);	
									isChecked = true;							
								}							
						  }						  
            }
          }
          }
	      );
	
	return false;
}

