/*add class=roll_image to a image for rollover effect*/
function roll_image() {
	jQuery('img.roll_image').hover(function() {
			var imageSrc = jQuery(this).attr("src");
			imageArr = imageSrc.split(".");
			// If roll is set replace it
			imageArr[imageArr.length - 2] = imageArr[imageArr.length - 2]+"_roll";
			var newImg = "";
			for(var i=0;i<imageArr.length;i++) {
				newImg += imageArr[i];
				if(i < imageArr.length-1) {
					newImg += ".";
				}
			}
			jQuery(this).attr("src", newImg);
		}, function() {
			var imageSrc = jQuery(this).attr("src");
			imageArr = imageSrc.split(".");
			imageArr[imageArr.length - 2] = imageArr[imageArr.length - 2].replace("_roll","");
			var newImg = "";
			for(var i=0;i<imageArr.length;i++) {
				newImg += imageArr[i];
				if(i < imageArr.length-1) {
					newImg += ".";
				}
			}
			jQuery(this).attr("src",newImg);
	});
}

/*rollover for buttons left/right fix and middle with a flexible width*/
function roll_button() {
	jQuery('.buttonbox,.buttonboxreader')
		.hover(
			function() {
				jQuery(this).addClass('button_roll');
			}, 
			function() {
				jQuery(this).removeClass('button_roll');
			})
		.mousedown(
			function() {
				jQuery(this).removeClass('button_roll').addClass('button_act');
			})
		.mouseup(
			function() {
				jQuery(this).removeClass('button_act');
			});
}

/*center a container horizontal*/
function centerElement(objId) {
	var myWidth =  jQuery('#'+objId).width();
	var parentWidth = jQuery('#'+objId).parent().width();
	var pos = Math.round(parentWidth/2)-Math.round(myWidth/2);
	jQuery('#'+objId).css({ left: pos+"px"});
}

/*top navigation livesite*/
function topNav() {
	//jQuery("#nav-global li:first").addClass('navleft');
	jQuery('#nav-global li')
		.mouseover(
			function() {
				jQuery(this).addClass('navroll');
			})
		.mouseout(
			function() {
				jQuery(this).removeClass('navroll'); 
			});
}

function showPopup(id) {
	var detailDiv = jQuery("#" + id);
	var windowObj = jQuery(window);
	var windowHeight = windowObj.height();
	//alert(windowObj.width()+' --- '+detailDiv.width());
	if (!document.getElementById("backgroundPopup")) {
		jQuery("body").append("<div id='backgroundPopup'><!-- --></div>");
	}
	jQuery("#backgroundPopup").show();
	
	
	detailDiv.css({left: (windowObj.width() - detailDiv.width()) / 2, top: (windowObj.height() - detailDiv.height()) / 2 + windowObj.scrollTop()}).fadeIn(250);
}

function hidePopup(id) {
	jQuery('#' + id).fadeOut(250, function(){jQuery(this).hide()});
	jQuery('#backgroundPopup').hide();
}

/* cookies */
function writeCookie(cname, cvalue, cexpire, domain) {
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+cexpire*365);    
	document.cookie = cname + '=' + escape(cvalue) +';' +'expires=' + exdate.toGMTString()+';path=/;domain='+domain+' ;' ;
}

function ReadCookie (cookieName) {
	var theCookie=''+document.cookie;
	var ind=theCookie.indexOf(cookieName);
	if (ind==-1 || cookieName=='') return ''; 
	var ind1=theCookie.indexOf(';',ind);
	if (ind1==-1) ind1=theCookie.length; 
	return unescape(theCookie.substring(ind+cookieName.length+1,ind1));
}

function GetLink(url) {
	if(ReadCookie('mediago')!='') {
		location.href=url;
		return true;
	} else {
		showPopup('checkmediago');
		return false;
	}
}

/*Default */
function defaultActions() {
	jQuery(".catalog_popularbox .resultElement:last").css("background-image", "none");
	jQuery(".blog_comment_innercontainer:last").css( {border : 'none', 'padding-bottom' : '0'} );
}

function showhidePS3 () {
      if (navigator.userAgent.toUpperCase().indexOf("PLAYSTATION 3") >= 0) {
            jQuery(".nonPs3").remove();
            jQuery(".isPs3").show();
      } else {
            jQuery(".isPs3").remove();
            jQuery(".nonPs3").show();
      }
}

function start() {
	topNav();
	roll_image();
	roll_button();
	defaultActions();
	showhidePS3 ();
};

window.onload = start;

// Logging FF etc console with check whether console is available
var consoleLog = function() {
	return {
		log : function(obj)	{
			if (window.console) {
				if (typeof(obj) == "object" || typeof(obj) == "array") {
					for (var i in obj) {
						console.log(i + ": " + obj[i]);
					}
				} else {
					console.log(obj);
				}
			}
		},
		dir : function(obj) {
			if (window.console) {
				console.dir(obj);
			}
		}
	};
} ();

