/**
 * This document should contain scripts that are unique for this site only
 */

if (typeof Panagora != 'object' || typeof Panagora == 'undefined')
	var Panagora = {};
	
if (typeof console != 'object')
	var console = { log: function() {}, info: function() {} }


var $j = jQuery.noConflict();

(function(){
	var map = {
		44: 'en',
		46: 'sv'
	};
	
	var hashSet = false;
	
	var revMap = {};
	for (var langid in map) revMap[map[langid]] = langid;
	
	var hashLang = location.hash.match(/lang=([a-z]{2})/);
	
	if (hashLang && hashLang.length == 2) {
		hashLang = hashLang[1];

		if (map[currentLanguage] != hashLang) {
			location.href = '/system/language/setlang.asp?langid=' + revMap[hashLang];
		}
	}
	else {
		location.hash += (location.hash.length ? '&' : '') + 'lang=' + map[currentLanguage];
	}

	hashSet = true;
	if(!$j.browser.safari){
		function checkHash() {
			if (hashSet && location.hash.indexOf('lang=') == -1)
				history.go(-1);
		}
	}
		 
	setInterval(checkHash, 250);
	
})();


$j(document).ready(function() {

	// Show zoom only if XL-image is actually XL
	var imgWidth = $j("#hugeImageHidden").width();
	var imgHeight = $j("#hugeImageHidden").height();
	//console.log(imgWidth);
	//console.log(imgHeight);	
	if(imgWidth >= 800 || imgHeight >= 800) {
		$j('#product-image-enlarge').show();
	}

	// shade overlay that goes right under the header
	$j('#wrapper #ie6-spawn-of-the-devil').append('<div id="shade-overlay"></div>');
	
	
	var url = new String(location.href);
	var gender = parseInt(url.substring(url.indexOf('b=') + 2, url.indexOf('b=') + 4));
	var cssClass = '';
	
	switch(gender){
		case 27:
			cssClass += ' class="male"';
		break;
		
		case 28:
			cssClass += ' class="female"';
		break;
	}
	// top decoration
	$j('#content').prepend('<div id="decoration"' + cssClass + '></div>');

	try {
		if($j('#login-email').length > 0){
			$j.fn.setInputFieldValueFromLabel($j('label[for="login-email"]').html(), '#login-email');
			$j.fn.setInputFieldValueFromLabel($j('label[for="login-password"]').html(), '#login-password');
		}
	
		$j.fn.setInputFieldValueFromLabel($j('label[for="newsletter-email"]').html(), '#newsletter-email');
		$j.fn.setInputFieldValueFromLabel($j('label[for="your-name"]').html(), '#your-name');
		$j.fn.setInputFieldValueFromLabel($j('label[for="friend-email"]').html(), '#friend-email');
	} catch (ex) {
	}


	/**
	* Function to set the new background-image on a product page
	*/
	$j('div.product-alt-image, div.product-alt-image-last').click(function(event){
		var id = this.id;
		$j('#product-main-image-link')
			.attr('href', productImages.baseUrl + "/" + productImages[id].huge);
		$j('#product-main-image-link img')
			.attr('src', productImages.baseUrl +'/'+ productImages[id].large)
	});
	
	$j('#product-main-image-link').click(function(e){
		e.preventDefault();
		
		var nextImageId = 0;
		var nextImage = 0;

		var currentImageId = 0;
		
		for (var i = 0; i < productImages.ids.length; i++) {
			if (this.getAttribute('href') == productImages.baseUrl + "/" + productImages[productImages.ids[i]].huge) {
				currentImageId = i;
			}
		}

		nextImage = (currentImageId >= productImages.ids.length-1) ? 0 : currentImageId + 1;
		
		$j(this).attr('href', productImages.baseUrl + "/" + productImages[productImages.ids[nextImage]].huge);
		$j(this).children().attr('src', productImages.baseUrl +'/'+ productImages[productImages.ids[nextImage]].large);

		return false;
	});

});


	



/**
	@param {DOMElement/string} toggleElement Clickable element (or id) with href containing the image link
	@param {Object} [options] See ModalWindow options (width, height and callbacks.hide will be overridden)
*/
Panagora.AddImageZoomAndStep = function(toggleElement, options) {
	function shutdown() {
		modalWindow.ShutDown();
	}

	var currentImageId = 0;

	function switchImg() {
		var nextImageId = 0;
		var nextImage = 0;
		
		for (var i = 0; i < productImages.ids.length; i++) {
			if (toggleElement.attr('href') == productImages.baseUrl + "/" + productImages[productImages.ids[i]].huge)
				currentImageId = i;
		}

		nextImage = (currentImageId >= productImages.ids.length-1) ? 0 : currentImageId + 1;
		
		var newImg = $j('<img/>')
			.attr('src', productImages.baseUrl +'/'+ productImages[productImages.ids[nextImage]].huge)
			.click(switchImg);

		modalWindow.SetContent(newImg);

		toggleElement
			.attr('href', productImages.baseUrl + "/" + productImages[productImages.ids[nextImage]].huge);
		toggleElement
			.children().attr('src', productImages.baseUrl +'/'+ productImages[productImages.ids[nextImage]].large);
	}

	function open(e) {
		e.stopPropagation();
		e.cancelBubble = true;
		
		// set options
		options.width = 0;
		options.height = 0;
		options.callbacks = options.callbacks || {};
		options.callbacks.hide = shutdown;


		// create window
		toggleElement.modalWindow = modalWindow = new Panagora.ModalWindow(options);
		
		// create image element
		var image = $j('<img src="/images/wait.gif"/>').get(0);

		// load progress animation
		modalWindow.SetContent(image);
		
		// load big image (with some insane IE workarounds)
		var imgObj = new Image();

		imgObj.onload = function () {
			img = $j('<img/>')
				.load(function () {
					var dims = {
						height: imgObj.height,
						width: imgObj.width
					};
					
					if (dims.width && dims.height) {
						modalWindow.SetOptions(dims);
					}
					
					img.click(switchImg); // click to close
			
					// add image
					modalWindow.SetContent(img);
			
					// show window
					modalWindow.Show();
				});
			
			img.attr('src', imgObj.src);
		}
		imgObj.src = toggleElement.attr('href');

		return false;
	}
	
	var modalWindow;
	var toggleElement;
	
	options = options || {};

	// get link element
	toggleElement = toggleElement.tagName ? $j(toggleElement) : $j('#' + toggleElement);
	
	// declare img variable for later use
	var img;
	
	$j(document).ready(function () {
		$j('#product-image-enlarge').click(open);
		$j('.infinite-carousel').infiniteCarousel(); 
		
	});
}