/**
 * Read the JavaScript cookies tutorial at:
 *   http://www.netspade.com/articles/javascript/cookies.xml
 */

/**
 * 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(c_name,value,expiredays)
{
	//if(expiredays=='') expiredays=1;
	//var exdate=new Date();
	//exdate.setDate(exdate.getDate()+expiredays);
	//document.cookie=c_name+ "=" +escape(value)+((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
	if (expiredays) {
			var date = new Date();
			date.setTime(date.getTime()+(expiredays*24*60*60*1000));
			var expires = "; expires="+date.toGMTString();
		}else{
			var date = new Date();
			date.setTime(date.getTime()+(1*24*60*60*1000));
			var expires = "; expires="+date.toGMTString();

		}
		document.cookie = c_name+"="+value+expires+"; path=/";
}
/**
 * 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);
    if (begin == -1)
    {
        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));
}

function initCookie(divid,cookiename){
	
	//var nodeList = document.getElementsByTagName('body')[0];
	//var gtbd = nodeList.innerHTML;
	//if (gtbd.indexOf(lich) < 0){
	//	alert(lcstr);
	//}
	if(cookieon==true)
	{
		var domain = '.malmoarena.com';
		var path = '/';
		
			var thecookie = getCookie(cookiename);
			if(thecookie==null){
					//var d=new Date();
					//var expdate=new Date();
					//expdate.setDate(expdate.getDate()+3);
					//setCookie(cookiename,d.getTime(),3);
					
					document.getElementById(divid).style.display = '';
					setCookie(cookiename, 'shown'); 
			}else{
				if(thecookie!='shown'){ //har den visats inom expireDate ska den INTE komma upp igen
					var d=new Date();
					var difference =  d.getTime() - thecookie;
					 var dayDiff = Math.floor(difference/1000/60/60/24);
					difference -= dayDiff*1000*60*60*24;
	
					var hourDiff = Math.floor(difference/1000/60/60);
					difference -= hourDiff*1000*60*60;
				 
					var minDiff = Math.floor(difference/1000/60);
					difference -= minDiff*1000*60;
					
					var secDiff = Math.floor(difference/1000);
	
					var launchit = false;
					if(dayDiff>0 || hourDiff >0 || minDiff>0) launchit = true;
					if(launchit==false && secDiff>40) launchit = true;
					if(launchit==true){
						
						document.getElementById(divid).style.display = '';
						setCookie(cookiename, 'shown'); 
						
					}
				}else{
						document.getElementById(divid).style.display = 'none';

				}
			}
	}
}

/**
 * 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))
    {
		//alert("tar bort cookie");
        document.cookie = name + "=" + 
            ((path) ? "; path=" + path : "") +
            ((domain) ? "; domain=" + domain : "") +
            "; expires=Thu, 01-Jan-70 00:00:01 GMT";
		//alert(getCookie(name));
    }
}


