var timeout    = 500;
var closetimer = 0;
var ddmenuitem = 0;

var $container = $('.bigpic').cycle({ 
		fx:    	'fade',
		activePagerClass: 'active',
	    speed:   1000, 
	    timeout: 5000,
	    shuffle: { top:'10px', left: '10px' } 
	});

if($("#modal"))
{
	
	$("#modal").hide().jqm().jqmShow().jqmAddClose('.hideDialog');
	
}

function jsddm_open()
{  jsddm_canceltimer();
   jsddm_close();
   ddmenuitem = $(this).find('ul').css('visibility', 'visible');}

function jsddm_close()
{  if(ddmenuitem) ddmenuitem.css('visibility', 'hidden');}

function jsddm_timer()
{  closetimer = window.setTimeout(jsddm_close, timeout);}

function jsddm_canceltimer()
{  if(closetimer)
   {  window.clearTimeout(closetimer);
      closetimer = null;}}

document.onclick = jsddm_close;


$(document).ready(function(){
	$('#lang_btn').click(function(){
	
		return false;
	});

	$('#main_nav > li').bind('mouseover', jsddm_open)
   $('#main_nav > li').bind('mouseout',  jsddm_timer)

	slideShow(5000);

	setHeight();
	
	if($('#searchbox'))
	{
		$('#searchbox').submit(function(){
			$('#reseller_list').load($(this).attr('action'),{'ajax':'true','searchquery': $('input[name=searchquery]').val()}, setHeight);
			return false;
		});
		
		$('#searchbox').keyup(function(){
			$('#reseller_list').load($(this).attr('action'),{'ajax':'true','searchquery': $('input[name=searchquery]').val()}, setHeight);
		});
	}
	
	if($('#file'))
	{
		$('#file').css({opacity: 0});
		$('#fileupload').show();

		$('#fileupload').click(function () {
			  $('#file').trigger('click');
		});
		$('#fileupload').select(function () {
			 $('#file').trigger('select');
		});
		
		$('#file').change(function () {
			 $('#fileupload').val($('#file').val());
		});
	}
	
	/* 
		
		Sorteringsfunktion
		Används i:
		Artikelmodulen för att sortera mellan män, kvinnor och barn 
	
	*/
	if('#sort')
	{
		$('#sort a').click(function() {  
  	      $(this).css('outline','none');  
  	      $('#sort a').not(this).removeClass('current');  
  	      $(this).addClass('current');  
  	       
  	      var filterVal = $(this).attr('rel');  
  	      
  	      if(filterVal == 'all') {  
  	          $('.product').fadeIn('slow');  
  	      } else {  
  	          	$('.product[rel*='+filterVal+']').delay(500).fadeIn(); 
  	          	$('.product:not([rel*='+filterVal+'])').fadeOut(); 
  	      }  
  	        
  	      return false;  
  	  }); 
	}
	
	if($('#form'))
	{
		$('#form').validate();
	}
	
	
	/*
	
		Presenterar thumbs i artikelpresentationen
		
	*/
	if($('.colorpics .img'))
	{
		
		$('.colorpics .img').hover(function(){
			
			var img = $(this).attr('src').replace('src(', '');
			
			img = img.replace('\"', '');
			img = img.replace('"', '');
			img = img.replace(')', '');
			
			$('#limg').attr('src', img);
			$('.big_pic').attr('href', img);
			
		});
		
		
		
	}
	
	if($('#limg'))
	{
		
		var options = {
	    zoomWidth: 300,
	    zoomHeight: 380,
            xOffset: 25,
            yOffset: 0,
            position: "left",
            showPreload: false,
            title: false
            
		};
		
		$('.big_pic').jqzoom(options);
	
	}
	
	//PLACEHOLDER
	// To detect native support for the HTML5 placeholder attribute
	var fakeInput = document.createElement("input"),
		placeHolderSupport = ("placeholder" in fakeInput),
		clearValue = function () {
			if (searchField.val() === originalText) {
				searchField.val("");
			}
		};
	
	// Applies placeholder attribute behavior in web browsers that don't support it
	if (!placeHolderSupport) {
		var searchField = $("input[placeholder]"),
			originalText = searchField.attr("placeholder");

		searchField.val(originalText);
		searchField.addClass("placeholder");

		searchField.bind("focus", function () {
			searchField.removeClass("placeholder");
			clearValue();
		});

		searchField.bind("blur", function () {
			if (searchField.val().length === 0) {
				searchField.val(originalText);
				searchField.addClass("placeholder");
			}
		});

		// Empties the placeholder text at form submit if it hasn't changed
		searchField.parents("form").bind("submit", function () {
			clearValue();
		});

		// Clear at window reload to avoid it stored in autocomplete
		$(window).bind("unload", function () {
			clearValue();
		});
	}
});



function setHeight()
{
	//alert($(document).height());
	//alert($('body').height());
	if($(document).height() > $('body').height() && !$.browser.msie)
	{
		$('body').css({"height": "100%"});
		$('html').css({"height": "100%"});
	}
	
}


function slideShow(speed) {
	//append a LI item to the UL list for displaying caption
	//$('ul.slideshow').append('<li id="slideshow-caption" class="caption"><div class="slideshow-caption-container"><h4></h4><p>&nbsp;</p></div></li>');
	//$('ul.slideshow').append('<li id="slideshow-caption" class="caption"><div class="slideshow-caption-container"><h4></h4><p>&nbsp;</p>');
	$('ul.slideshow').append('<li id="slideshow-caption" class="caption"><div class="slideshow-caption-container"><h4></h4>');

	//Set the opacity of all images to 0
	$('ul.slideshow li').css({opacity: 0.0});
	
	//Get the first image and display it (set it to full opacity)
	$('ul.slideshow li:first').css({opacity: 1.0});
	
	//Get the caption of the first image from TITLE attribute and display it
	$('#slideshow-caption h4').html($('ul.slideshow a:first').find('img').attr('title'));
	//Get the caption of the first image from ALT attribute, use it to show the avatar	
	//$('#slideshow-caption p').html($('ul.slideshow a:first').find('img').attr('alt'));
	var avatarpic = $('ul.slideshow a:first').find('img').attr('alt');
	$('#slideshow-caption').append('<img src="' + avatarpic + '" width="50" height="50" class="avatar" /></div></li>');

		
	//Display the caption
	$('#slideshow-caption').css({opacity: 1, bottom:10});
	
	//Call the gallery function to run the slideshow	
	var timer = setInterval('gallery()',speed);
	
	//pause the slideshow on mouse over
	//$('ul.slideshow').hover(
	//	function () {
	//		clearInterval(timer);	
	//	}, 	
	//	function () {
	//		timer = setInterval('gallery()',speed);			
	//	}
	//);
}

function gallery() 
{
	
	//if no IMGs have the show class, grab the first image
	var current = ($('ul.slideshow li.show')?  $('ul.slideshow li.show') : $('#ul.slideshow li:first'));

	//Get next image, if it reached the end of the slideshow, rotate it back to the first image
	var next = ((current.next().length) ? ((current.next().attr('id') == 'slideshow-caption')? $('ul.slideshow li:first') :current.next()) : $('ul.slideshow li:first'));
		
	//Get next image caption
	var title = next.find('img').attr('title');	
	var desc = next.find('img').attr('alt');	
		
	//Set the fade in effect for the next image, show class has higher z-index
	next.css({opacity: 0.0}).addClass('show').animate({opacity: 1.0}, 1500);
	
	//Hide the caption first, and then set and display the caption
	$('#slideshow-caption').animate({opacity:0}, 500, function () 
	{
			//Display the content
//			$('#slideshow-caption h4').html(title);
//			$('#slideshow-caption p').html(desc);				
//			$('#slideshow-caption').animate({opacity:1}, 1000);	
			
			$('#slideshow-caption h4').html(title);
			$('#slideshow-caption img').replaceWith('<img src="' + desc + '" width="50" height="50" class="avatar" />');
						
			$('#slideshow-caption').animate({opacity:1}, 1000);	
			
			
	});		

	//Hide the current image
	current.animate({opacity: 0.0}, 1500).removeClass('show');

}

/* 
	#### SUBNEWS ####
	Visar vald subnews i valt objekt.
	Funktionen triggas vid respektive subnews länk (rubrik, bild eller liknande).
	Är en tidigare vald subnews redan vald skall denne tas bort innan nya posten skall visas. 
*/
function show_subnews(sub_id)
{

	var sub_id			=	sub_id;
	var sub_headline	=	$('#sub_' + sub_id).find('strong').text();
	var sub_content		=	$('#sub_' + sub_id).find('input').attr('value');
	
	// Släcker först .subnews om den visas
	//$('.subnews').html('');
	$('.subnews').slideUp('slow');
	
	// Presenterar det nya innehållet
	$('.subnews').slideDown('slow', function() {
		
		// Sätter innehållet.
		$('.subnews').html('<h2>' + sub_headline + '</h2>' + sub_content + '<p><a href="#" onClick="hide_subnews()">Stäng »</a></p><br /><span class="clear"></span>');
		
	});
	

		
}
/* 
	#### SUBNEWS ####
	Stänger valt objekt.
*/
function hide_subnews()
{
	
	$('.subnews').slideUp('slow',function(){
		
			$('.subnews').slideUp('slow').html('');
		
		});
	
}


// Nu hanterar vi innehåll indelade i klickbara tabbar 
$('.tabs a').click(function(){
	switch_tabs($(this));
	return false;
});

switch_tabs($('.defaulttab'));
	
function switch_tabs(obj)
{
	
	$('.tab-content').hide();
	$('.tabs a').removeClass("selected");
	var id = obj.attr("href");
			 
	$(id).show();
	obj.addClass("selected");
		
	return false;
	
}

// Nu hanterar vi innehåll indelade i klickbara formaterade (CSS3) tabbar 
$('.tabbed a').click(function(){
	
	switch_tabbed($(this));
	
	// Gömmer objektet subnews om det är synligt
	$('.subnews').hide();
	
	return false;
	
});

switch_tabbed($('.defaulttab'));
	
function switch_tabbed(obj)
{
	$('.tabbed-content').hide();
	$('.tabbed a').removeClass("selected");
	var id = obj.attr("href");
	 
	$(id).show();
	obj.addClass("selected");
	
	return false;
	
}

// Nu hanterar vi innehåll indelade i klickbara formaterade (CSS3) tabbar liknande en vanlig sub-meny 
$('.links a').click(function(){
	switch_links($(this));
	return false;
});

switch_links($('.defaulttab'));
	
function switch_links(obj)
{
	
	$('.links-content').hide();
	$('.links a').removeClass("selected");
	var id = obj.attr("href");
	 
	$(id).show();
	obj.addClass("selected");
	
	return false;
	
}
