// ------------------------------------------------
// ------- pomocne funkce pro cookies -------------
// ------------------------------------------------

function createCookie(name,value) {
	var 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 "";
}

// -------------------------------------------

function eraseCookie(name) { createCookie(name,"",-1); }

// -------------------------------------------

function getCount(name){
	var pValue = readCookie(name);	// precteni cookies
	
	if((pValue != "") && (pValue != null)){						
		cArray = new Array();
		cArray = pValue.split(",");	// rozsekani na jednotlive polozky				
		cArrayCount = (cArray.length);		
	}
	else{ cArrayCount = 0; }	
	return cArrayCount;
}

// -------------------------------------------

function inCookie(name, pId){	
	var pValue = readCookie(name);	// precteni cookies
	
	if((pValue != "") && (pValue != null)){				
		cArray = new Array();
		cArray = pValue.split(",");	// rozsekani na jednotlive polozky								
	
		// prochazeni vsech polozek
		if(cArray.length != 0){
			for(i=0; i<=cArray.length; i++){			
				if( pId == cArray[i] ){ return true; }
			}
		}
		return false;										
	}
	else{ return false; }
}

// ------------------------------------------------
// ------- parsovani cookies ----------------------
// ------------------------------------------------

function addValue(name, pId){
	var pValue = readCookie(name);
	
	// vytvor retezec pro ulozeni
	if( getCount(name)!=0 ){ pValue = pValue + "," + pId }
	else{ pValue += pId; }
	
	// zapis cookie
	createCookie(name, pValue);			
}	

// -------------------------------------------

function delValue(name, pId){
	var pValue = readCookie(name);	// precteni cookies
	
	if((pValue != "") && (pValue != null)){	
		cArray = new Array();			
		cArray = pValue.split(",");	// rozsekani na jednotlive polozky		
		
		// je polozka v cookies?
		if( inCookie(name, pId) ){				
			
			// prochazeni vsech polozek a pozadovanou smazem
			for(i=0; i<cArray.length; i++){ if( pId == cArray[i] ){ cArray.splice(i,1); } }												
			
			// vygenerovani noveho retezce a zapis
			newValues = cArray.join(",");				
			createCookie(name, newValues);
																			
			return true;
		}
		else{ return false; }																
	}
	else{ return false; }		
}

function updateItemsCount(name){
	var s = "";
	
	if( getCount(name)!=0 ){ s = "("+ getCount(name) +")"; }
	else{ s = ""; }
	
	$("span.count").text( s );		
}

// ------------------------------------------------
// ------- jQuery ---------------------------------
// ------------------------------------------------

$(document).ready(function(){

var elCartEnv = "#compareCart";
var elCart = "#compareList-in";
var elProducts = ".product";
var elProductDetail = "#productActions";
	
var cName = "comparsion";
var cSaveVal = "";	
var cVal = "";

// pokud cookie neexistuje tak vytvor prazdnou
if( readCookie(cName) == null ){ 
	eraseCookie(cName);
	createCookie(cName, "");
}
// vytvoreni menu
createMenu(cName);
updateItemsCount(cName);

// ------------------------------------------------
// ------- menu -----------------------------------
// ------------------------------------------------

// vytovreni polozky ze seznamu
function addMenuItem( pId ){			
	var item = $('<div class="item" id="i-'+ pId +'"></div>');
	var removeLink = document.createElement("a");
		removeLink.innerHTML = "x";
        removeLink.className = "del";
        removeLink.href = "#";
        removeLink.onclick = function(){
        	delValue(cName, pId);
			delMenuItem( pId ); 	// smazani z cookie a kosiku				
			return false;			        
        }
		
	// jmeno polozky k pripojeni	
	sId = "input[id="+ pId +"]";	
	sName = $(elProducts).find(sId).parents(elProducts).find("h2").text();
	sHref = $(elProducts).find(sId).parents(elProducts).find("h2 a").attr("href");	
	
	sNewItem = '<a href="'+ sHref +'">'+ sName +'</a>';
		
	item.append( removeLink ).append( sNewItem );		
	// pripojeni polozky k menu
	$(elCart).append(item);	
	updateItemsCount( cName );		
}

// ------------------------------------------------

// vytovreni polozky z detailu
function addMenuItem2( pId ){			
	var item = $('<div class="item" id="i-'+ pId +'"></div>');
	var removeLink = document.createElement("a");
		removeLink.innerHTML = "x";
        removeLink.className = "del";
        removeLink.href = "#";
        removeLink.onclick = function(){
        	delValue(cName, pId);
			delMenuItem( pId ); 	// smazani z cookie a kosiku				
			return false;			        
        }
		
	// jmeno polozky k pripojeni			
	sName = $("#product").find("h1").text();
	sHref = $(".compare").find("#url").val();	
	
	sNewItem = '<a href="'+ sHref +'">'+ sName +'</a>';
		
	item.append( removeLink ).append( sNewItem );		
	// pripojeni polozky k menu
	$(elCart).append(item);	
	updateItemsCount( cName );		
}

// ------------------------------------------------

// smazani polozky z menu 
function delMenuItem( pId ){	
	$(elCart).find(".item").each(function(){
		if( $(this).attr("id") == "i-" + pId ){
			$(this).remove(); 
		}
	});		
	
	// skryti porovnavaci bubliny pokud je prazdna
	if( getCount(cName) == 0){ $("#compareList").hide(); }
	
	// seznam produktu
	$(elProducts).find("input").each(function(){
		if( $(this).attr("id") == pId ){
			$(this).removeAttr("checked");
		}
	});
	
	// detail clanku
	$(".compare").find("input").each(function(){
		if( $(this).attr("id") == pId ){
			$(this).removeAttr("checked");
		}
	});	
	
	updateItemsCount( cName );				
}

// ------------------------------------------------

function createMenu(name){								
	var pValue = readCookie(name);
		
	if((pValue != "") && (pValue != null)){
		cArray = new Array();			
		cArray = pValue.split(",");	// rozsekani na jednotlive polozky		
										
		$(elProducts).find("input[type=checkbox]").removeAttr("checked");
			
		// prochazeni vsech polozek
		for(i=0; i<cArray.length; i++){
			
			// vygeneruj menu jen pokud nebylo vygenerovano v sablone 
			if( $(elCart).hasClass("generate") == false ){
				addMenuItem( cArray[i] );
			}
			
			sId = "input[id="+ cArray[i] +"]";			
			$(elProducts).find(sId).attr("checked","checked");									
			$(elProductDetail).find(sId).attr("checked","checked");
		}								
	}			
}

// ------------------------------------------------
// ------- udalosti -------------------------------
// ------------------------------------------------

var elStr = elProducts +" input[type=checkbox], .compare input[type=checkbox]";

// pridavani ze seznamu
$(".compare input[type=checkbox]").click(function(){

	tVal = $(this).attr("id");
	
	// pokud neni hodnota v cookies tak...
	if( inCookie(cName, tVal) == false ){		
		if( getCount(cName) < 5 ){ 			
			addValue(cName, tVal);	// zapsani do cookies						
			
			if( $(this).hasClass("chbDetail") == true ){
				addMenuItem2(tVal);	// vytvoreni polozky menu					
			}
			else{
				addMenuItem(tVal);	// vytvoreni polozky menu									
			}
			
															
			$(this).effect("transfer", { to: $(elCartEnv) }, 300);
		}
		else{
			alert("V porovnání může být max 5 položek");
			return false;		
		}
	}
	// pokud existuje tak polozku smaz
	else{ 
		delValue(cName, tVal);
		delMenuItem(tVal);
								
		$(elCartEnv).effect("transfer", { to: $(this) }, 300);
	}

});

// -------------------------------------------

$(".del").click(function(){

	pId =  $(this).parent().attr("id");
	pId = pId.substring(2);
	
	delValue(cName, pId);
	delMenuItem( pId ); 	// smazani z cookie a kosiku												
	return false;

});

$(elCartEnv).hover(
	function(){
		if( $(this).find(".item").size() != 0 ){
			$(this).find("#compareList").show();
			$(this).find("#compareList .item:odd").addClass("odd");
		}
	},
	function(){
		$(this).find("#compareList").hide();
	}
);

});

