function trim(str)
{
    if(!str || typeof str != 'string')
        return null;

    return str.replace(/^[\s]+/,'').replace(/[\s]+$/,'').replace(/[\s]{2,}/,' ');
}

function getCookie( name ) {
var start = document.cookie.indexOf( name + "=" );
var len = start + name.length + 1;
if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) ) {
return null;
}
if ( start == -1 ) return null;
var end = document.cookie.indexOf( ';', len );
if ( end == -1 ) end = document.cookie.length;
return unescape( document.cookie.substring( len, end ) );
}

function setCookie( name, value, expires, path, domain, secure ) {
var today = new Date();
today.setTime( today.getTime() );
if ( expires ) {
expires = expires * 1000 * 60 * 60 * 24;
}
var expires_date = new Date( today.getTime() + (expires) );
document.cookie = name+'='+escape( value ) +
( ( expires ) ? ';expires='+expires_date.toGMTString() : '' ) + //expires.toGMTString()
( ( path ) ? ';path=' + path : '' ) +
( ( domain ) ? ';domain=' + domain : '' ) +
( ( secure ) ? ';secure' : '' );
}

function deleteCookie( name, path, domain ) {
if ( getCookie( name ) ) document.cookie = name + '=' +
( ( path ) ? ';path=' + path : '') +
( ( domain ) ? ';domain=' + domain : '' ) +
';expires=Thu, 01-Jan-1970 00:00:01 GMT';
}

function isCookiesEnabled() { 
var r = false;
setCookie('test','ok',1,'/','','');
if (getCookie('test') != null) {
r = true;
deleteCookie('test','/','');
}
return r;
} 

function getLang() {
var l_lang;
if (navigator.userLanguage) // Explorer
l_lang = navigator.userLanguage;
else if (navigator.language) // FF
l_lang = navigator.language;
else
l_lang = "en";
return l_lang;
}

function autoLRedirect(){
var target = 'de'; //must in lowercase
var lang = getLang();
if(lang.toLowerCase().indexOf(target) != -1){
setCookie('reURL','http://www.bleux.eu/de/intro.html',7,'/','','');
window.location.href = 'http://www.bleux.eu/de/intro.html';
}else{
setCookie('reURL','http://www.bleux.eu/en/intro.html',7,'/','','');
window.location.href = 'http://www.bleux.eu/en/intro.html';
}
//alert(getCookie('reURL'));
}

function changeImg(src_url, target_id, name, oid, target_id2){
if(src_url.length>0){
src_url = src_url.replace("thumb.gif","main.gif");
$(target_id).attr("src", src_url);
var new_url = $("#name"+oid+" > a:first").attr("href");
var addToFav_link = $("#fav"+oid).html();
$(target_id2).html('<a href="#">'+name+'</a> '+addToFav_link);
$(".product_code").text($("#code"+oid).text());
$(".large_colorway").lightBox();
}
}

function change_href(target_id, new_href){
$(target_id).attr("href", new_href);
}

function addToFavourite(objectId) {
	var objectType = 35;
	var result = CMS.FavoriteProcess.ServerSideAddToFavorites(objectId, objectType);
	if(result){
		alert("Successfully Added To your Favourites List");
	}
	//result is boolean, true or false
}

$("body").ready(function(){

$(".top_lvl_item").bind("mouseover", function(){
	var pos = $(this).position();
	var left = pos.left;
	var top = pos.top;
	$(this).find(".submenu:first").css("left", left);
	$(this).find(".submenu:first").css("top", top+34);
	$(this).find(".submenu:first").show();
});

$(".top_lvl_item").bind("mouseout", function(){
	$(this).find(".submenu").hide();
});

});