/*
	-- Common javascript functions --
	
	-- Author: 		e950
	-- URL:			http://www.e950.lv/
	-- Modified: 	02.10.2008
*/

$(document).ready(function() {
	
	// -- Content minheight
	
	$(window).bind('resize', function() {
		$('#body').css('min-height', ($(window).height() - 134) + 'px');
    }).trigger('resize');

	
	// -- Content slider
	/*$("#nav li a[href*=#]").click(function () {
		
		var id = $(this).attr('href').split("#");
		
		if($(".subcontentholder:hidden #"+id[1]+"").attr('id')) {
			$(".contentholder:eq(0)").hide();
			$(".subcontentholder").show();
		}
		
		var traget = $("#"+id[1]).offset();
		$(document).scrollTo(parseInt(traget.top - (traget.top <= 113 ? 113 : 120)), 700);
		return false;		
	});*/
	
	if(window.location.href.search(/brand-strategy/i) != -1) {
		$("#nav li a[href$=brand-strategy/]").click(function() {
			$(".subcontentholder").hide();
			$(".contentholder:eq(0)").show();
			return false;
		});
	}
	
	//-- Read more
	var readmore = $("a[rel=readMore]");
	if($(readmore).attr('rel')) {
		$(".subcontentholder").hide();
		$(".contentholder:eq(0) a, #nav li ul a").click(function () {
			$(".contentholder:eq(0)").hide();
			$(".subcontentholder").show();
		});
	}
	
	// -- Gallery
	var gallerypos = $(".contentholder").position();
	$(".hoverimg").css({ top: gallerypos.top  + 2, left: gallerypos.left });
	
	$(".hoverimg .placeholder, .hoverimg .placeholder2").hover(function(){
		var id = $(this).attr('id').substr(1);

		if($("#f"+id).css('top') == '0px' || $("#f"+id).css('top') == 'auto' || !$("#f"+id).css('top')) {
			var pos = $(this).attr('class').substr(15, 2);
			var nr = parseInt($(this).attr('class').substr(19));
			var maxheight = parseInt($(".hoverimg").height()) + 5;
			var maxwidth = $(".hoverimg").width();
			var containerheight = $(".hoverimg").position();
			if($(this).hasClass('placeholder2')) {
				var contwidth = 154;
			} else {
				var contwidth = 78;
			}
			
			$("#f"+id+", #f"+id+" img").show();		
			
			containerheight.top = containerheight.top - 4;
			
			var location = $("#h"+id).position();

			location.top = containerheight.top + location.top + 2;
			location.left = containerheight.left + location.left;

			while(parseInt(location.left - containerheight.left + parseInt($("#f"+id).width())) > maxwidth) {
				location.left = location.left - contwidth;
			}
			
			if(location.top != 0 && location.top != 120) {
				while(parseInt(location.top - containerheight.top + parseInt($("#f"+id).height())) > maxheight) {
					location.top = location.top - contwidth;
				}
			}
			//-34
			
			$("#f"+id).css({top: location.top, left: location.left, zIndex: 100, display: 'block' });
		} else {
			$("#f"+id).show();
		}
		
		
		
	}, function() {
		var id = $(this).attr('id').substr(1);
		$("#f"+id).fadeOut("fast");
	});
	
	// -- Read more class fix
	var readmorew = $(".readmore").width();
	var activew = $(".active").width();
	$(".readmore").css({
		display: 'block',
		width: readmorew + 1
	});
	$(".active").css({
		display: 'block',
		width: activew + 1
	});
	
	// -- Drop down
	$("#header a img[alt=brandbox]").click(function(){
		$("#drop").show();
		$("body:not(#drop)").unbind('click');
		$("body:not(#drop)").click(function(){
			$("#drop").hide();
			$("body:not(#drop)").unbind('click');
		});
		return false
		
	});

});