
// Define console to prevent errors in IE

if(typeof console == "undefined") {
	var console = {
		log: function () {},
		dir: function () {}
	};
}
else if(console.log == "undefined") {
	console.log = function () {};
}
else if(console.dir == "undefined") {
	console.dir = function () {};
}


$(document).ready(function() {
	$('#slider').anythingSlider({
		startText           : "Start",   // Start button text
		stopText            : "Stop",    // Stop button text
		hashTags            : false,
		startStopped    	: false, 	 // If autoPlay is on, this can force it to start stopped
		autoPlay            : true,      // This turns off the entire slideshow FUNCTIONALY, not just if it starts running or not
		delay               : 6000,      // How long between slideshow transitions in AutoPlay mode (in milliseconds)
		animationTime       : 1000,       // How long the slideshow transition takes (in milliseconds)
				
		onSlideComplete 	: function(slider){
			// alert('Welcome to Slide #' + slider.currentPage);
		}
	});
	
	$('.sidebar_slideshow').anythingSlider({
		width               : 216,      // Override the default CSS width
		height              : null,      // Override the default CSS height
		hashTags            : false,
		buildNavigation     : false,
				
		onSlideComplete 	: function(slider){
			// alert('Welcome to Slide #' + slider.currentPage);
		}
	});
	
	
	
	
	// Swoosh update
	updateSwooshPosition();
	$(window).resize( function() {
		updateSwooshPosition();	
	});
	
	// Frontpage update
	updateFrontpageSidebar();
	

	// Searchfield
	var searchTexts = {
		'fin' : 'Hae verkkokaupasta',
		'rus' : 'Поиск товара',
		'eng' : 'Product search'	
	}
	
	var classes = $('#pageContainer').attr('class').split(' ');
	var siteaccess = 'fin'; //default is finnish
	
	$(classes).each(function() {

		if(this.indexOf('siteaccess') >= 0 ) {
			siteaccess = this.split('siteaccess-')[1];
		}
	});
		
	$('#search .searchText').val(searchTexts[siteaccess]);	
	
	$('#search .searchText').focus(function () {
		// if default text, remove on focus	
		if( $(this).val().indexOf(searchTexts[siteaccess]) === 0 ) {
			$(this).val('');
		}
		
		$('#search').addClass('active');
	});	
	
	$('#search .searchText').blur(function () {
		
		if($(this).val().length === 0) {
			$(this).val(searchTexts[siteaccess]);
		}
		
		$('#search').removeClass('active');
	});
	
	// Open search results in new window
	$('#search').submit(function() {

		var url = 'http://www.elkome.fi/verkkokauppa/advanced_search_result.php';
		var query = '';
		
		$(this).find('input').each(function(index) {
			
			
			if($(this).attr('name')) {
				if(index > 0) { query += '&'; }
				query += $(this).attr('name');
				query += '=';
				query += $(this).val();
			}
		});
		
		window.open(url + '?' + query);
		
		return false;	
		
	});
	
	/*
		Break out from iframe in language navigation (needed for Google translate)
	*/
	
	$('#langNavigation a').click(function() {
		if (top.location!= self.location) {
			top.location = $(this).attr('href');
			return false;
		}
	});
	
	
	/*
		Front page box
	*/
	
	$('.frontBox .menu li').mouseover(function(e) {
		
		var siblings = $(this).siblings();
		siblings.removeClass('active');
		
		$('.frontBox .slide').addClass('hidden');	
		
		var slide = $(this).attr('id').split('link-')[1];
		$('#' + slide).removeClass('hidden');
		$(this).addClass('active');		
		
	});
	
	$('.frontBox').mouseleave(function(e) {
		$('.frontBox .slide').addClass('hidden');
		$('.frontBox #slide-default').removeClass('hidden');
		$('.frontBox .menu li').removeClass('active');

	});
	
	if (top.location!= self.location) {
		$('#pageContainer').addClass('in-iframe');
	}

	
});

/*
	Updates the visibility of swoosh element in page
*/
function updateSwooshPosition() {
	var elem = $('#headerContainer');
	var min = parseInt(elem.css('min-width'));
	var max = parseInt(elem.css('max-width'));
	var cur = elem.width();

	if(cur < max) {
		var pos = (cur - max)/2 + 'px 0px';
		elem.css('background-position', pos);
	} else {
		elem.css('background-position', '0px 0px');
	}	
}

function updateFrontpageSidebar() {

	if( $('#frontpageSidebar').length > 0) {
		var maxHeight = 0;
		$('#frontpageSidebar').find('.s-content').each( function() {
			maxHeight = Math.max( maxHeight, $(this).height());	
		} );
		$('#frontpageSidebar').find('.s-content').each( function() {
			$(this).height(maxHeight);			
		});
		
	}	

}
