if (Mediastruktur == undefined) var Mediastruktur = {};


/* START SIFR--------------------- */
var garamond = {
	src: '/static/js/lib/gara_sudd2.swf'
	// Set ratios for better loading
	,ratios: [9,1.27,14,1.19,21,1.16,28,1.13,38,1.12,53,1.11,60,1.1,61,1.11,94,1.1,96,1.09,101,1.1,102,1.09,103,1.1,108,1.09,110,1.1,1.09]
	};

sIFR.useStyleCheck = false;
//sIFR.forceClear = true;
//sIFR.useDomLoaded = false;
//sIFR.fitExactly = true;
sIFR.activate(garamond);

//sIFR.redraw();

// Calculate ratios:
//sIFR.debug.ratios({ src: 'static/js/lib/gara_read.swf', selector: 'div#content h1 span' });

// Replace div#content h1.price span with flash
sIFR.replace(garamond, {
	transparent: true, selector: 'div#content h1.price span'
	//,forceSingleLine: true
	,tuneHeight: -4
	,offsetTop: 1
	,css: [
		 '.sIFR-root { color: #282828; letter-spacing: 0.5; }'
		,'a { text-decoration: none; }'
		,'a:link { color: #FFFFFF; }'
		,'a:hover { color: #CCCCCC; }'
	]
});

sIFR.replace(garamond, {
	transparent: true, selector: 'div#content div.faded_black h1 span'
	//,forceSingleLine: true
	,tuneHeight: -3
	,css: [
		 '.sIFR-root { color: #ffffff; letter-spacing: 0.5; }'
		,'a { text-decoration: none; }'
		,'a:link { color: #FFFFFF; }'
		,'a:hover { color: #CCCCCC; }'
	]
});


// Replace div#content h1 span with flash
sIFR.replace(garamond, {
	transparent: true, selector: 'div#content h1 span'
	//,forceSingleLine: true
	,tuneHeight: -3
	,css: [
		 '.sIFR-root { color: #282828; letter-spacing: 0.5; }'
		,'a { text-decoration: none; }'
		,'a:link { color: #FFFFFF; }'
		,'a:hover { color: #CCCCCC; }'
	]
});


// Replace div#content h2 span with flash
sIFR.replace(garamond, {
	transparent: true, selector: 'div#content h2 span'
	//,forceSingleLine: true
	,tuneHeight: -4
	,offsetTop: 1
	,css: [
		 '.sIFR-root { color: #282828; letter-spacing: 0.5; }'
		,'a { text-decoration: none; }'
		,'a:link { color: #FFFFFF; }'
		,'a:hover { color: #CCCCCC; }'
	]
});

sIFR.replace(garamond, {
	transparent: true, selector: 'div#content h3 span'
	//,forceSingleLine: true
	,tuneHeight: -4
	,offsetTop: 1
	,css: [
		 '.sIFR-root { color: #282828; letter-spacing: 0.5; }'
		,'a { text-decoration: none; }'
		,'a:link { color: #FFFFFF; }'
		,'a:hover { color: #CCCCCC; }'
	]
});

/* ----------------------------- */

function replaceSIFR() {
// Replace div#content h2 span with flash
sIFR.replace(garamond, {
	transparent: true, selector: 'div#content h2 span'
	//,forceSingleLine: true
	,tuneHeight: -4
	,offsetTop: 1
	,css: [
		 '.sIFR-root { color: #282828; letter-spacing: 0.5; }'
		,'a { text-decoration: none; }'
		,'a:link { color: #FFFFFF; }'
		,'a:hover { color: #CCCCCC; }'
	]
});
	
}



/*
 *	Class	Mediastruktur.Fader
 *	Require Prototype.js 1.6
 *
 *	Fading system for Skärgårdstunnan
 *
 */
Mediastruktur.Fader = Class.create({
	initialize: function() {
		try {
			this.duration					= 1.5;
			this.images 					=	[];
			this.container 				= $("productImages")
			this.thumbs_container	= $("productImage")
			this.anchors 					= $A(this.container.getElementsByTagName("a"));
			this.divs							= $A(this.container.getElementsByTagName("div"));
			this.anchors_length 	= this.anchors.size();
	
			/// For each anchor ... And bind "this" ///
			this.anchors.each(function(item, i) {
				// Set event handlers on all anchors and bind them to onClick
				Event.observe(item, 'click', this.onClick.bindAsEventListener(this), false);
				// Push huge image into array
				this.images.push((new Element('img', {'src': item.href})).hide());
			}, this);
		} catch(e) {
			// Do nothing
		}
	},
	// Used when images are clicked
	onClick: function(event) {
		// Scroll to bigger picture first.
		Effect.ScrollTo($("productImage"));
		
		this.this_obj				= Event.element(event);
		this.a 						= Event.element(event).up('a');
		this.href 					= this.a.href;
		this.hidden_img				= (new Element('img', {'src': this.href})).hide();
		// Set all divs to 
		this.divs.each(function(item, i) {
			item = $(item);
			item.removeClassName("selected");
		}, this);
		$(this.this_obj.up('div')).addClassName("selected");
		//this.this_obj.hide();
		//Effect.Appear(this.this_obj, { duration: 0.5 });
		// Append to container
		this.thumbs_container.appendChild(this.hidden_img);
		Effect.Appear(this.hidden_img,  { duration: this.duration });
		// Remove first child
		Element.remove.delay(this.duration,this.thumbs_container.firstDescendant());
		// Stop anchors event, like return false;
		Event.stop(event);
	}
});

Mediastruktur.Misc = Class.create({
	initialize: function() {
		this.setWindowsClass();
		this.observerNavigator();
	},
	
	/*
	 * If using Windows, set new class on #container
	 */
	setWindowsClass: function() {
		this.isWindows			= navigator.platform.toLowerCase().include("win");
		if (this.isWindows) {
			$("container").addClassName("windows");
		}
	},
	
	observerNavigator: function() {
		this.listItems							= $("navigator").childElements();
		this.listItems.each(function(item, i) {
			Event.observe(item, 'click', function(event) {
				try {
					this.listItem						= Event.findElement(event, 'li');
					this.item_href					= this.listItem.getElementsByTagName("a")[0].getAttribute("href");
					document.location.href 	= this.item_href;
					Event.stop(event);	
				} catch(e) {
					//Do nothing
				}
			});
		}, this);
	}
});

// Newsticker Class, cred: Bartelme.at
newsticker = Class.create();
newsticker.prototype = {
	initialize: function()
	{
		// Get elements
		this.interval = 6000;
		this.container = $("news");
		this.messages  = $A(this.container.getElementsByTagName("li"));
		this.number_of_messages = this.messages.length;
		if (this.number_of_messages == 0)
		{
			this.showError();
			return false;
		}
		this.current_message = 0;
		this.previous_message = null;
		this.hideMessages();
		this.showMessage();
		this.container.show();
		// Install timer
		this.timer = setInterval(this.showMessage.bind(this), this.interval);
  	},
	showMessage: function()
	{
		Effect.Appear(this.messages[this.current_message]);
		setTimeout(this.fadeMessage.bind(this), this.interval-2000);
		if (this.current_message < this.number_of_messages-1)
		{
			this.previous_message = this.current_message;
			this.current_message = this.current_message + 1;
		} else {
			this.current_message = 0;
			this.previous_message = this.number_of_messages - 1;
		}
	},
	fadeMessage: function()
	{
		Effect.Fade(this.messages[this.previous_message]);
	},
	hideMessages: function()
	{
		this.messages.each(function(message)
		{
			Element.hide(message);
		})
	},
	showError: function()
	{
		if (this.container.getElementsByTagName("ul").length == 0)
		{
			this.list = document.createElement("ul");
			this.container.appendChild(this.list);
		} else {
			this.list = this.container.getElementsByTagName("ul")[0];
		}
		this.errorMessage = document.createElement("li");
		this.errorMessage.className = "alert";
		this.errorMessage.innerHTML = "Could not retrieve data";
		this.list.appendChild(this.errorMessage);
	}
}

Event.observe(window, 'load', function() {
	//OnLoad, DO ..
	new newsticker();
	new Mediastruktur.Misc();
	new Mediastruktur.Fader();
	setupZoom();
});

var totalCreditCost = 0;

function addCreditCost(cost, obj) {
	obj = $(obj);
	this.result1 = $("result1");
	this.result2 = $("result2");
	this.result3 = $("result3");
	this.result4 = $("result4");
	
	/* If checked add amount */
	if (obj.checked) {
		totalCreditCost = totalCreditCost + cost;
	} else {
		totalCreditCost = totalCreditCost - cost;
	}
	/* Set results */
	if (totalCreditCost != 0 ) {
		this.result1.update(Math.ceil(totalCreditCost/6)+" :-");
		this.result2.update(Math.ceil(totalCreditCost/12)+" :-");
		this.result3.update(Math.ceil(totalCreditCost/18)+" :-");
		this.result4.update(Math.ceil(totalCreditCost/24)+" :-");
		new Effect.Highlight(this.result1,{ startcolor: '#ffff99', endcolor: '#f2f2f2', restorecolor: '#f2f2f2' });
		new Effect.Highlight(this.result2,{ startcolor: '#ffff99', endcolor: '#ffffff', restorecolor: '#ffffff' });
		new Effect.Highlight(this.result3,{ startcolor: '#ffff99', endcolor: '#f2f2f2', restorecolor: '#f2f2f2' });
		new Effect.Highlight(this.result4,{ startcolor: '#ffff99', endcolor: '#ffffff', restorecolor: '#ffffff' });
		
	} else {
		this.result1.update("");
		this.result2.update("");
		this.result3.update("");
		this.result4.update("");
	}
	
	return false;
}