/**
 * Gets the value of the specified cookie.
 *
 * name  Name of the desired cookie.
 *
 * Returns a string containing value of specified cookie,
 *   or null if cookie does not exist.
 */
function getCookie(name)
{
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix); //namirame ';'
    if (begin == -1) //ako ?:ne e namerilo tova na niakoe mesto:?
    {
        begin = dc.indexOf(prefix);
        if (begin != 0) return null;
    }
    else
    {
        begin += 2;
    }
    var end = document.cookie.indexOf(";", begin);
    if (end == -1)
    {
        end = dc.length;
    }
    return unescape(dc.substring(begin + prefix.length, end));
}

/**
 * Sets a Cookie with the given name and value.
 *
 * name       Name of the cookie
 * value      Value of the cookie
 * [expires]  Expiration date of the cookie (default: end of current session)
 * [path]     Path where the cookie is valid (default: path of calling document)
 * [domain]   Domain where the cookie is valid
 *              (default: domain of calling document)
 * [secure]   Boolean value indicating if the cookie transmission requires a
 *              secure transmission
 */
function setCookie(name, value, expires, path, domain, secure)
{
    document.cookie= name + "=" + escape(value) +
        ((expires) ? "; expires=" + expires.toGMTString() : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
}


/**
 * Deletes the specified cookie.
 *
 * name      name of the cookie
 * [path]    path of the cookie (must be same as path used to create cookie)
 * [domain]  domain of the cookie (must be same as domain used to create cookie)
 */
function deleteCookie(name, path, domain)
{
    if (getCookie(name))
    {
        document.cookie = name + "=" + 
            ((path) ? "; path=" + path : "") +
            ((domain) ? "; domain=" + domain : "") +
            "; expires=Thu, 01-Jan-70 00:00:01 GMT";
    }
}

/**
 * Sets a Cookie with the given name and value.
 *
 * name       Name of the cookie
 * value      Value of the cookie
 * [expires]  Expiration date of the cookie (default: end of current session)
 * [path]     Path where the cookie is valid (default: path of calling document)
 * [domain]   Domain where the cookie is valid
 *              (default: domain of calling document)
 * [secure]   Boolean value indicating if the cookie transmission requires a
 *              secure transmission
 */
function setCmCookie(name, value, expires, path, domain)
{
	oldCookie=getCookie(name);
	if(oldCookie!= null)
	{
    	cookieValue=(oldCookie.length>0)?oldCookie:'';
	}
	else
		cookieValue='';
	//imame li takova
    var found = -1; //priemame che go niama
	value=value+'.';

	//tyrsim dali ne e v nachaloto
	if(value===cookieValue.slice(0, value.length))
	{
	   	cookieValue=cookieValue.slice(value.length,cookieValue.length);
	}
	else
	{
		//  inache go tyrsim i s tochka otpred
		if (value && cookieValue)
		{	
			found=cookieValue.indexOf('.' + value);
		}
	    //dobavi 
	    if (found == -1)
	    {
	    	cookieValue=cookieValue + value;
	    }
	    //ili iztrii
	    if (found > -1)
	    {
	    	cookieValue=cookieValue.slice(0, found) + cookieValue.slice(found+value.toString().length, cookieValue.length);
	    }
	}
	document.cookie= name + "=" + escape(cookieValue) +
        ((expires) ? "; expires=" + expires.toGMTString() : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "");
    
   //broim tochkite, ako imame >0, togava pishem kolko sa, inache pishem 0
	itemnum=0;
	for(i=0;i<cookieValue.length;i++)
	{
		if(cookieValue.charAt(i)=='.')
		{
			itemnum++;
			continue;
		}
		continue;
	}   
   
	if(document.getElementById("numElements") != undefined) document.getElementById("numElements").innerHTML=itemnum;
	if(document.getElementById("numElements2") != undefined) document.getElementById("numElements2").innerHTML=itemnum;

}

/**
* @return bool,int
* @param unknown $cookie_name
* @param unknown $divider
* @desc Proveriava kolko elementa ima v cookieto
*/
function items_number($cookie_name,$divider)
{
	oldCookie=getCookie($cookie_name);
	if(oldCookie== null || oldCookie.length<2 ) return false;
	count = 0;
	pos = oldCookie.indexOf($divider);
	while( pos != -1 ) 
	{   
		count++;
		pos = oldCookie.indexOf($divider,pos+1);
	}
	return count;
}


/**
* @return bool
* @desc Proveriava dali ima minimum 2 elementa za sravnenie i vrysha true/false
*/
function check_items()
{
	$numb=items_number('items','.');
	if($numb===false || $numb<2)
	{
		alert('Изберете поне 2 продукта за сравнение!');
		return false;
	}
	return true;
}

var show;
function showmenu()
{
  var el = document.getElementById("channels");
  var iframe_menu = document.getElementById('iframe_menu');
  if (el) {
    el.style.visibility = "visible";
      iframe_menu.style.width = el.offsetWidth;
      iframe_menu.style.height = el.offsetHeight;
      iframe_menu.style.top = el.style.top;
      iframe_menu.style.left = el.style.left;
      iframe_menu.style.zIndex = el.style.zIndex - 1;
      iframe_menu.style.display = "block";
    show = 1;
  }
}
function hidemenu()
{
  show = 0;
  setTimeout("hide()", 600);
}

function hide()
{
  var el = document.getElementById("channels");
  var iframe_menu = document.getElementById('iframe_menu');
  if (el) {
    if (!show)
    {
      el.style.visibility = "hidden";
      iframe_menu.style.display = "none";
    }
  }
}