$(document).ready(function() {
	//$('#menu li.item').click(function(){
	//	if($(this).children('div.sub')){
	//		var sub = $(this).children('div.sub');
	//		sub.slideDown(200);
	//	}	
	//});
	//$('#menu li.item div.sub').mouseout(function(){
	//		$(this).slideUp(200);
	//});
	
	$('.modal').mouseover(function(e){
		e.preventDefault();
		$('.modalbox').css({'display':'none'});
		var id = $(this).attr('id');
		$('#hidden'+id).remove();
		$('body').append('<input type="text" name="hidden" id="hidden'+id+'" value="0" />');
		var boxId = $('#'+id+'_content');
		$('#'+id+'_modal').remove();
		$('body').append('<div id="'+id+'_modal" class="modalbox"></div>');
		$(this).mouseleave(function(){
			setTimeout(function(){
				if($('#hidden'+id).val()=='0'){
					modal.mouseleave();
				}
			}, 2000);	// Murry: that's the time the script will wait
						// for the user to hover the modal box in miliseconds. 
						// 2000 stands for 2 seconds. I think it's enought for most users.
		});
		var offset = $(this).offset();	
		var height = $(this).height();
		var modal = $('#'+id+'_modal');
		if($(this).attr('rel')){
			modal.css('width', $(this).attr('rel')+'px');
		}
		modal.html(boxId.html());

		if(id=='brandsmenu') {
			modal.css({'position': 'absolute', top: (offset.top+5+height)+'px', left: offset.left-230});
		} else if(id=='brandsmenusearch') {
			modal.css({'position': 'absolute', top: (offset.top+5+height)+'px', left: offset.left-330});
		} else {
			modal.css({'position': 'absolute', top: (offset.top+5+height)+'px', left: offset.left-5});
		}
		modal.fadeIn(100);
		modal.mouseenter(function(){
			$('#hidden'+id).val('1');			
		});
		modal.mouseleave(function(){
			$(this).fadeOut(100, function(){
				$(this).remove();
			});
		});
	});
	
	$('#slides a').click(function(e){
		e.preventDefault();
		var currentId = $('#slides a.active').attr('id');
		$('#'+currentId+'_content').fadeOut(400);
		$('#'+currentId).removeClass('active');
		currentId = $(this).attr('id');
		$(this).addClass('active');
		$('#'+currentId+'_content').fadeIn(400);
	});
	

	$('a.mpButton').click(function(e){
		e.preventDefault();
		var way = null;
		var itemsCount = $('a.mpItem').length;
		var itemsWidth = itemsCount*152;
		var currentLeft = $('#mpHolderWall').css('left');
		var currentLeft = currentLeft.replace('px', '');
		var currentLeft = Number(currentLeft);
		var newLeft = 0;
		if($(this).hasClass('up')) way = 'up';
		if($(this).hasClass('down')) way = 'down';
		if(way=='up'){
			newLeft = currentLeft+152;
		}else{
			newLeft = currentLeft-152;
		}
		if(newLeft < (0-(itemsWidth-152))) newLeft = 0;
		if(newLeft > 0) newLeft = 0-((itemsWidth-152));
		$('#mpHolderWall').animate({left: newLeft+'px'}, 300);
	});


	$('a.brandsButton').click(function(e){
		e.preventDefault();
		var way = null;
		var itemsCount = $('a.brandsItem').length;
		var itemsWidth = (itemsCount*157)-(2*157);
		var currentLeft = $('#brandsHolderWall').css('left');
		var currentLeft = currentLeft.replace('px', '');
		var currentLeft = Number(currentLeft);
		var newTop = 0;
		if($(this).hasClass('up')) way = 'up';
		if($(this).hasClass('down')) way = 'down';
		if(way=='up'){
			newLeft = currentLeft+157;
		}else{
			newLeft = currentLeft-157;
		}
		if(newLeft < (0-(itemsWidth-157))) newLeft = 0;
		if(newLeft > 0) newLeft = 0-((itemsWidth-157));
		$('#brandsHolderWall').animate({left: newLeft+'px'}, 300);
	});
	
	$('#indexTabs a').click(function(e){
		e.preventDefault();
		var currentId = $('#indexTabs a.active').attr('id');
		$('#'+currentId+'_content').css('display', 'none');
		$('#'+currentId).removeClass('active');
		currentId = $(this).attr('id');
		$(this).addClass('active');
		$('#'+currentId+'_content').fadeIn(400);
	});
	
});


function autoSlide(){
	var currentId = $('#slides a.active').attr('id');
	$('#'+currentId+'_content').fadeOut(400);
	$('#'+currentId).removeClass('active');
	
	if(currentId=='slide1') { currentId='slide2'; }
	else if(currentId=='slide2') { currentId='slide3'; }
	else if(currentId=='slide3') { currentId='slide4'; }
	else if(currentId=='slide4') { currentId='slide1'; }
	$('#'+currentId).addClass('active');
	$('#'+currentId+'_content').fadeIn(400);
}

window.onload=new Function('setInterval("autoSlide()",5000)');

/** new footer **/
function stickFooter(){
		positionFooter(); 
		function positionFooter(){
			$("#newsletterFooter").css({position: "absolute",top:($(window).scrollTop()+$(window).height()-$("#newsletterFooter").height())+"px"});	
		}
	 
		$(window).scroll(positionFooter).resize(positionFooter)
		
		$('#newsletter_rightside_close').click(function(e){
			e.preventDefault();
			$.get("/style/rememberNewsletter/"); // if you want to save the user choise in session - opens the URL with the GET method
			$('#newsletterFooter').fadeOut(400, function(){
				$(this).remove();	
			});
		});
}

