function configureHoverableImages(){
	$(".hoverableBgImg").live('mouseover mouseout', function (event) {
		var imageName, imageExtension, newImgSrc, imgSrc;
		imgSrc = $(this).css('background-image');
		imgSrc = imgSrc.replace('url("', '');
		imgSrc = imgSrc.replace("url('", '');
		imgSrc = imgSrc.replace('")', '');
		imgSrc = imgSrc.replace("')", '');
		imgSrc = imgSrc.replace('url(', '');
		imgSrc = imgSrc.replace(')', '');

		if (event.type === 'mouseover') { //onmouseover
			
			if (!$(this).hasClass('hovered')) {

				imageName = imgSrc.substr(0, imgSrc.length - 4);
				imageExtension = imgSrc.substr(imgSrc.length - 3, 3);
				newImgSrc = imageName + '_over' + '.' + imageExtension;
				$(this).addClass('hovered');
				var tmpImg = 'url(' + newImgSrc + ')';
				$(this).css('background-image', tmpImg);
			}
		} else { //onmouseout
			if ($(this).hasClass('hovered')) {
				imageName = imgSrc.substr(0, imgSrc.length - 9);
				imageExtension = imgSrc.substr(imgSrc.length - 3, 3);
				newImgSrc = imageName + '.' + imageExtension;
				$(this).removeClass('hovered');

				$(this).css('background-image', 'url(' + newImgSrc + ')');
			}
		}
	});
}

function configureLinkableItems(){
	$(".linkable").live("mouseover", function (event) {
        $(this).css("cursor", "pointer");
    });
}

function rotateImg() {	
	//Get the first image
	var current = ($('.bannerImg ul li.show')?  $('.bannerImg ul li.show') : $('.bannerImg ul li:first'));
 
	//Get next image, when it reaches the end, rotate it back to the first image
	var next = ((current.next().length) ? ((current.next().hasClass('show')) ? $('.bannerImg ul li:first') :current.next()) : $('.bannerImg ul li:first'));	
	
	//Set the fade in effect for the next image, the show class has higher z-index
	next.css({opacity: 0.0})
	.addClass('show')
	.animate({opacity: 1}, 1000);
 
	//Hide the current image
	current.animate({opacity: 0.0}, 1000)
	.removeClass('show');
	
}

function rotateBannerImg() {
	
	//Set the opacity of all images to 0
	$('.bannerImg ul li').css({opacity: 0.0});	
	//Get the first image and display it (gets set to full opacity)
	$('.bannerImg ul li:first').css({opacity: 1});		
	//Call the rotator function to run the slideshow, 6000 = change to next image after 6 seconds
	setInterval(rotateImg,5000);
	
}

function initMenus() {
	$('div.main_menu ul ul').hide();
	
	$.each($('.main_menu ul.menu'), function(){
		$('#' + this.id + '.expandfirst ul:first').show();
	});
	$('div.main_menu>ul.menu a, #footer p>a.pageLink, div.signup').click(
		function(e) {
		
			e.preventDefault();
			
			if($(this).attr('stamp') != "" && this.parentNode.parentNode.id == 'main_menu'){
				$("#main_menu>li>a").each(function(){
					if($(this).attr('stamp') == ""){
						if($(this).attr('stamp') == "" && ($(this).next().css('display') != 'none')){
							$(this).trigger('click');
						}
					}
				});
			}
			
			var checkElement = $(this).next();
			var parent = this.parentNode.parentNode.id;
				
			
			
			
			if((checkElement.is('ul')) && (checkElement.is(':visible'))) {
				
				if($('#' + parent).hasClass('collapsible')) {
					$('#' + parent + ' ul:visible').slideUp('normal');
				}
				
			}
			
			if((checkElement.is('ul')) && (!checkElement.is(':visible'))) {
				$('#' + parent + ' ul:visible').slideUp('normal');
				checkElement.slideDown('normal');				
			}
			
			if ($(this).attr("stamp") !== "")
			{
				var newPage = $(this).attr("page_name");
				
				var currentHeight  = $("#content .page_content .content").height();
				$("#content .page_content .content").css("height",currentHeight+"px");
				$("#content .page_content .content .current_content").html("");				
				$("#content .page_content .loading").css("top",(currentHeight-35)/2 + "px");
				$("#content .page_content .loading").show();
				var currentPage = $(this).attr("stamp");
				var that = this;
				$.get("/getPageContent-pages/"+language+":"+$(this).attr("stamp")+"/",function(data){
					if (data.success){
						_gaq.push(['_trackPageview', '/' + language + '-pages/' + $(that).attr("stamp")+"/" ]);
						$("#content .page_content .loading").hide();
						$("#content .page_content .content .current_content").html("<h1>"+data.title+"</h1>"+data.content);
						$("#content .page_content .content").css("height","auto");
						document.title="Sport Akademia";
						if (data.title!==""){
							document.title = data.title + " - Sport Akademia" ;
						}
						if(history.replaceState){
							history.replaceState(null, currentPage, '/' + language + '-pages/' + currentPage + '/');
						}

					}else{
						alert('An error occured, please contact webmaster@sportakademia.com');
					}
				});
			}
		}
	);
}

$(document).ready(function () {

	configureHoverableImages();
	configureLinkableItems();
	rotateBannerImg();
	initMenus();
});


