/*/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

 romfisk 2010
 website by eDIR/ Atelier Espen Schjelderup © All Rights Reserved 2010-2110

 carousel initialization

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////*/
$(document).ready(function() {
	/*////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	 configurable parameters
	////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////*/
	var intro_fadein = 1000;

	var carousel_anim_speed = 700;
	var carousel_easing = "easeInOutExpo"; // (alternates: "easeOutBack", "easeInOutQuint")

	var gap = 20; // carousel content: margin between image and text
	var offset = 80; // reportion: offset factor (larger value > objects appear closer to each other)

	var e_box_mtb = 50; // e_box: margin top, bottom
	var e_box_mlr = 50; // e_box: margin left, right
	var e_box_veil_opacity = 0.96;
	var e_box_veil_fadein_dur = 200;
	var e_box_box_fadein_dur = 400;
	var e_box_veil_fadeout_dur = 100;
	var e_box_box_fadeout_dur = 200;
	var e_box_veil_color = "#f7f7f7";
	var e_box_class_name = "overlay";
	var e_box_closer_class_name = ".closer"
	var e_box_loader_img = "/core/graphics/loader.gif";
	var e_box_veil_to_close = true;
	var e_box_box_to_close = false;

	var e_box_video_w = 612;
	var e_box_video_h = 378;
	var e_box_video_loader_img = "/core/graphics/loader_video.gif";

	/*////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	 global varibles
	////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////*/
	var viewport_h = $(window).height();
	var vp_half_h = parseInt(viewport_h / 2);
	var viewport_w = $(window).width();
	var vp_half_w = parseInt(viewport_w / 2);
	var mod_h = $('#carousel ul li').height();
	var mod_half_h = parseInt(mod_h / 2);
	var mod_w = $("#focus").width();
	var mod_half_w = parseInt(mod_w / 2);
	var total = $('#carousel ul li').length;

	var e_box_w = viewport_w - e_box_mlr;
	var e_box_h = viewport_h - e_box_mtb;

	var $ie6 = ($.browser.msie && ($.browser.version == "6.0")) ? true : false;

	/*////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	 fade in content
	////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////*/
	var $carousel = $("#carousel ul");
	$carousel.hide();
	$carousel.fadeIn(intro_fadein);

	/*/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	 window resize - reloads page after 500 milliseconds, on resize
	/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////*/
	$(window).resize(function() {
		if( this.resizeTO ) {
			clearTimeout(this.resizeTO);
		}
		this.resizeTO = setTimeout(function() {
			$(this).trigger('resizeEnd');
		}, 500); // adjust number to set delay of resize trigger
	});

	$(window).bind('resizeEnd', function() {
		location.reload();
	});

	/*////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	 image/text placement
	////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////*/
	var n = 1;

	$("#carousel ul li").each(function() {
		var img_w = $('#i' + n).width();
		var img_half_w = parseInt(img_w / 2);
		var img_h = $('#i' + n).height();
		var img_half_h = parseInt(img_h / 2);

		var txt_treshold = mod_half_w + 10;
		var txt_left_pos = mod_half_w + img_half_w + gap;

		var txt_w;
		if( vp_half_w < txt_treshold ) {
			var avail_w = vp_half_w - img_half_w - gap - 10;
			txt_w = avail_w;
		}
		else {
			txt_w =  mod_half_w - img_half_w - gap;
		}

		$("#i" + n).css({'width' : img_w, 'margin-left' : -img_half_w, 'height' : img_h, 'margin-top' : -img_half_h, 'visibility' : 'visible'});
		$("#t" + n).css({'left' : txt_left_pos, 'visibility' : 'visible', 'width' : txt_w});

		var txt_h = $("#t" + n).height();
		var div_txt = parseInt(txt_h / 2);
		$("#t" + n).css({'height' : txt_h,  'margin-top' : -div_txt});

		n++
	});

	/*////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	 carousel
	 reportion : calculate visible portion of next/previous entry at top/bottom of viewport (modify margins)
	////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////*/
	var reportion = {
		num_sort: function(a, b) {
			if( a < b ) {
				return -1;
			}
			else if( a > b ) {
				return 1;
			}
			return 0;
		},
		find_median: function(x) {
			x.sort( reportion.num_sort );
			var _length = x.length;
			var middle = Math.floor(_length / 2);
			var median;
			if( (_length % 2) != 0 ) {
				median = x[middle];
			}
			else {
				median = parseInt((x[middle - 1] + x[middle]) / 2);
			}
			return median;
		},
		init: function() {
			var a = vp_half_h;
			var b = mod_half_h;
			var $c = [];
			var d = offset;

			var i = 1;
			while( i <= total ) {
				var img_h = $('#i' + i +' img').height();
    				$c.push(parseInt((mod_h - img_h) / 2));
				i++;
			}

			c = reportion.find_median($c);
			x = a - b - c - d;

			if( x > 2 ) {
				$("#carousel ul li").css({'margin-bottom' : x});
			}
			else {
				$("#carousel ul li").css({'margin-bottom' : 2});
			}
		}
	}

	reportion.init();

	/*////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	 init: jcarousel lite
	 26.1.2012 - added hash functionality to the carousel
	////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////*/
	if( window.location.hash ) {
		var fresh_hash = window.location.hash.substring(1); //puts hash in variable, and removes the # character
		var load_hash = fresh_hash - 1;
	}

	var pattern = /[0-9]+/g;

	$("#carousel").jCarouselLite({
		vertical: true,
		mouseWheel: true,
		easing: carousel_easing,
		speed: carousel_anim_speed,
		start: 0,
		hashload: load_hash, // option hashload added to module 31.1.2012
		visible: 3,
		btnPrev: "#up",
		btnNext: "#down",
		afterLoad: function(a) {
     			$('*').find(".txt").hide(1);
			$(a[0]).find(".txt").fadeIn(1);
			var get_hash = $(a[0]).attr("id");
			var this_hash = get_hash.match(pattern); // compiles a hash value from the ID of the parent (li)
			window.location.hash = this_hash;
		},
 		beforeStart: function(a) {
 			$(a[0]).find(".txt").fadeOut(carousel_anim_speed);
			var $fade = $('#news_bubble');
			if( $fade.css("display") != "none"  ) {
				$fade.fadeTo(950, '0.0', function() {
					$fade.hide();
				});
			}
		},
		afterEnd: function(a) {
 			$(a[0]).find(".txt").fadeIn(100);
			var get_hash = $(a[0]).attr("id");
			var this_hash = get_hash.match(pattern); // compiles a hash value from the ID of the parent (li)
			window.location.hash = this_hash;
		}
	});

	/*////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	 init: e_box (productsheet, videosheet)
	////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////*/
	$("a.open_spec").e_box({
		width: e_box_w,
		height: e_box_h,
		class_name: e_box_class_name,
		loader_img: e_box_loader_img,
		opacity: e_box_veil_opacity,
		closer_class_name: e_box_closer_class_name,
		veil_bg_color: e_box_veil_color,
		box_fadein_speed: e_box_box_fadein_dur,
		veil_fadein_speed: e_box_veil_fadein_dur,
		box_fadeout_speed: e_box_box_fadeout_dur,
		veil_fadeout_speed: e_box_veil_fadeout_dur,
		veil_close: e_box_veil_to_close,
		box_close: e_box_box_to_close
	});

	$("a.open_vid").e_box({
		width: e_box_video_w,
		height: e_box_video_h,
		class_name: e_box_class_name,
		loader_img: e_box_video_loader_img,
		opacity: e_box_veil_opacity,
		closer_class_name: e_box_closer_class_name,
		veil_bg_color: e_box_veil_color,
		box_fadein_speed: e_box_box_fadein_dur,
		veil_fadein_speed: e_box_veil_fadein_dur,
		box_fadeout_speed: e_box_box_fadeout_dur,
		veil_fadeout_speed: e_box_veil_fadeout_dur,
		veil_close: e_box_veil_to_close,
		box_close: e_box_box_to_close
	});

	/*////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	 init : cufón font replacement
	////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////*/
	Cufon.replace('h2');

	/*////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	 jcarousel arrow:hover effects
	////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////*/
	$("#up, #down").hover(function () {
		$(this).css("background-position", '0 -33px');
		var $fade = $(this);
		if ($fade.is(':animated')) {
			$fade.stop().fadeTo(100, '1.0');
		}
		else {
			$(this).fadeTo(100, '1.0');
		}
	}, function () {
		$(this).css("background-position", '0 -33px');
		var $fade = $(this);
		if ($fade.is(':animated')){
			$fade.stop().fadeTo(400,'0.4');
		}
		else {
			$(this).fadeTo(400, '0.4');
		}
	});

	/*///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	 ie6 clip fix for scroll container
	///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////*/
	if( $ie6 ) {
		var $css_obj = {
			"position": "absolute",
			"top": vp_half_h
		}
		$('#focus').css($css_obj);

	}

	/*////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	 init: news ticker
	////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////*/
	$("#news").newsTicker(7500);

	$("#bubble_closer").click(function () {
		var $fade = $('#news_bubble');
		$fade.fadeTo(300, '0.0');
	});
});
