// Settings
var motion_speed = 700;
var slide_width = 802;
var slide_height = 335;

// Set Gfx Object
$.fn.gfx_object = function(src, f){

	// Image object
	if (src.lastIndexOf(".swf") == -1) {
		
		return this.each(function(){
			var i = new Image(slide_width, slide_height);
			i.src = src;
			i.onload = f;
			$(this).html(i);
			
			$("#slide_container").fadeIn("fast");
		});
		
	// Swf object
	} else {
		
		return this.each(function(){			
			
			// Empty current content
			$(this).empty();			
			
			// Determine browser
			var useragent = navigator.userAgent.toLowerCase();
			var browser = useragent.indexOf('msie 6') > 0 || useragent.indexOf('msie 7') > 0 || useragent.indexOf('msie 8') > 0 ? false : true;		
						
			if (!browser) {
				
				// Object
				var obj = '<embed src="' + src + '" width="' + slide_width + '" height="' + slide_height + '"></embed>';
				
				// Add new content
				document.getElementById("slide_container").innerHTML = obj;

			} else {
				
				// Object
				var obj = document.createElement("object");
				obj.setAttribute("type", "application/x-shockwave-flash");
				obj.setAttribute("width", slide_width);
				obj.setAttribute("height", slide_height);
				obj.setAttribute("data", src);
				
				// Param
				var param_a = document.createElement("param");
				param_a.setAttribute("name", "movie");
				param_a.setAttribute("value", src);
				obj.appendChild(param_a);				
				
				// Embed
				var embed = document.createElement("embed");
				embed.setAttribute("width", slide_width);
				embed.setAttribute("height", slide_height);
				embed.setAttribute("type", "application/x-shockwave-flash");
				embed.setAttribute("src", src);
				obj.appendChild(embed);
				
				// Add new content
				document.getElementById("slide_container").appendChild(obj);				
			}
			
			// Show content
			$("#slide_container").css("display", "block");
		});
	}
}

// Get current slider position
function get_slider_pos () {
	
	return $("div#slide_options").css("marginLeft");
}

// Document ready
jQuery(document).ready(function(){	
	
	// Slider settings
	var new_marginleft = 0;
	var start_marginleft = parseInt($("div#slide_options").css("marginLeft"));
	var current_marginleft = start_marginleft;
	var margin_hor_step = 149;
	var slide_amount = $("div#slide_options > img.slide").length;
	var max_marginleft = 0-(slide_amount*margin_hor_step) + (1*margin_hor_step);
	var active_slide = null;
	var center_slide_nr = 3;
	var current_center_slide_nr = center_slide_nr;	
	
	// Previous slide nav button
	$("img#slide_nav_previous").click(function (){
		
		new_marginleft = current_marginleft+margin_hor_step;

		if (new_marginleft <= start_marginleft) {

			$("div#slide_options").stop().animate({"marginTop":"0", "marginLeft":new_marginleft}, motion_speed, "easeOutBack");
			
			current_marginleft = new_marginleft;
			
			current_center_slide_nr--;
		}
	});
	
	// Next slide nav button
	$("img#slide_nav_next").click(function (){
		
		new_marginleft = current_marginleft-margin_hor_step;

		if (new_marginleft > max_marginleft) {

			$("div#slide_options").stop().animate({"marginTop":"0", "marginLeft":new_marginleft}, motion_speed, "easeOutBack");
			
			current_marginleft = new_marginleft;
			
			current_center_slide_nr++;
		}
	});
	
	// Slide handler	
	$("img.slide").click(function () {

		$("#slide_container").css("display", "none");

		// Get selection data [nr_src]
		var selection_data = $(this).attr("rel").split('_');
		var current_slide_nr = selection_data[0];
		var new_src = selection_data[1];
		var spec_width = selection_data[2];
		var spec_height = selection_data[3];
		var spec_depth = selection_data[4];
		var product_title = selection_data[5];
		
		// Remove previous selection
		if (active_slide != null) {
			
			$(active_slide).removeClass("selected_slide");
			active_slide = null;
		}
			
		// Show current selection
		$(this).addClass("selected_slide");
			
		// Change slider nav position so the selection will be in center position
		if (slide_amount > center_slide_nr) {
			
			change_pos_count_to_center = current_slide_nr - current_center_slide_nr;
			
			new_marginleft = current_marginleft-(change_pos_count_to_center*margin_hor_step);

			if (new_marginleft > max_marginleft) {
			
				$("div#slide_options").stop().animate({"marginTop":"0", "marginLeft":new_marginleft}, motion_speed, "easeOutBack");
				
				current_marginleft = new_marginleft;
				
				current_center_slide_nr = current_slide_nr;
			}
		}
		
		// Change object
		$("#slide_container").gfx_object(new_src ,function(){
			
			// Ready statement
			$("#product_title").text(": "+product_title);
			$("#spec_width").text(spec_width);
			$("#spec_height").text(spec_height);
			$("#spec_depth").text(spec_depth);
		});
		
		active_slide = this;
	});	
	
	// News Ticker
	$("#news").newsTicker();

	// Product images
	$("#showroom_product_detail .images .thumb_image img#thumb1").parents("div:first").addClass("selected_thumb_con");
	$("#showroom_product_detail .images .large_image img#img1").fadeIn("fast");
	
	// Mouse action
	$("#showroom_product_detail .images .thumb_image div").mouseover(function(){
		
		// Determine image ID from thumb
		img_id = $(this).children("img").attr("id");
		img_id = img_id.replace("thumb", "");
	
		// Set new selected thumb
		$("#showroom_product_detail .images .thumb_image div").removeClass("selected_thumb_con");
		$(this).addClass("selected_thumb_con");
	
		// Hide current image
		$("#showroom_product_detail .images .large_image img").css("display","none");
		
		// Show larger image
		$("#showroom_product_detail .images .large_image #img"+img_id).fadeIn("fast");
	});	
})

// Set content height dependent on the menu
window.onload = set_content_height;

function set_content_height () {
	
	var content_height = parseInt(document.getElementById('main').offsetHeight);
	var left_nav_height = parseInt(document.getElementById('left_nav').offsetHeight)+50;
	
	if (content_height < left_nav_height)
		$('#main').css('height', left_nav_height+'px');
}

// Shadow box
Shadowbox.init({
    handleOversize: "drag",
    modal: true,
    overlayOpacity: "0.7"
});

