/*
*******************************************

skolplagget.se
General JavaScript functions

Created by gocrash MEDIA and Improove
http://www.gocrash.se
http://www.improove.se

*******************************************
*/


$j(document).ready(function(){


	$j('a.zoom').fancyZoom();
	
	
	
	$j('select.shop-by').change(function() {
		location.href=$j(this).val();
	});
	
	
	$j(".homebox-headers ul li").live("click", function(){
		clearInterval(step_interval);
		var nr = $j(this).attr("id").split("homebox-header-");
		show_contents(nr[1]);
	});
	
	$j(".homebox-dots ul li").live("click", function(){
		clearInterval(step_interval);
		var nr = $j(this).attr("id").split("homebox-dot-");
		show_contents(nr[1]);
	});
	
	
	$j(".kundservice-boc-header").click(function() {
		customerServiceExpand($j(this));
	});
	
	if($j(".homebox-container").length>0) {
		var step_interval = setInterval(function() {
			step_content();
		}, 4000);
	}

});

function customerServiceExpand(obj){
	var slide_id = obj.attr("id").split("header-");
	$j("#slide-"+slide_id[1]).toggle();

	if(obj.css("background-image").indexOf("-close")==-1) {
		obj.css("background-image",obj.css("background-image").replace("-open","-close"));
	}
	else {
		obj.css("background-image",obj.css("background-image").replace("-close","-open"));
	}	
}


/* ================================= STARTSNURRA ================================= */

var homebox_contents = new Array();
var current_content = 0;

function load_headers() {

	$j.getJSON('/skolplagget/article/service/articleheaders?limit=5', function(data) {
		
		
		var i = 0;
		for (i=0;i<data.length;i++){
			var class_active = "";
			if(i==0) {
				class_active = " class=\"active\"";
			}

				
			$j(".homebox-headers ul").append("<li id=\"homebox-header-"+i+"\""+class_active+">"+data[i]['title']+"</li>");
			homebox_contents[i] = new Array();
			homebox_contents[i]['id'] = data[i]['id'];
			homebox_contents[i]['title'] = data[i]['title'];



		}		
		load_contents(0);
	});

}

function load_contents(nr) {
	$j.getJSON('/skolplagget/article/service/articlebyid?id='+homebox_contents[nr]['id'], function(data) {
		homebox_contents[nr]['html'] = data['html'];
		homebox_contents[nr]['image'] = data['image'];
		
		if(nr==0) {
			show_contents(nr);
		}
		
		if(nr<4) {
			load_contents(nr+1);
		}
	});
}
	
function show_contents(nr) {

	if(homebox_contents[nr]['html']!=undefined) {
		current_content = nr;
		$j(".homebox-content-header h1").html(homebox_contents[nr]['title']);
		$j(".homebox-content-image img").attr("src",homebox_contents[nr]['image']);
		$j(".homebox-content-description p").html(homebox_contents[nr]['html']);
		$j(".homebox-dots ul li").removeClass("active");
		$j(".homebox-dots ul li#homebox-dot-"+nr).addClass("active");
		$j(".homebox-headers ul li").removeClass("active");
		$j(".homebox-headers ul li#homebox-header-"+nr).addClass("active");
	}
	
	
	
}

function step_content() {
	if(current_content<4) {
		current_content=current_content+1
	}
	else {
		current_content = 0;
	}
	show_contents(current_content);
}

var spFB = {
    isInitialized:false,
    rootElement:null,
    render:function() 
    {
	if(this.isInitialized)
	    return;

	if(this.rootElement == null) {
	    this.rootElement = document.createElement('DIV');
	    this.rootElement.setAttribute('id', 'fb-root');
	    var htmlBody = document.getElementsByTagName('body')[0];
	    htmlBody.insertBefore(this.rootElement, htmlBody.firstChild);	    
	}

	window.fbAsyncInit = function() {
	    FB.init({status: true, cookie: true, xfbml: true});
	};
	(function() {
	  var e = document.createElement('script'); e.async = true;
	  e.src = document.location.protocol +
	    '//connect.facebook.net/sv_SE/all.js';
	  document.getElementById('fb-root').appendChild(e);
	}());

	this.isInitialized = true;
    }
}

/* ================================= TRYCKTEKNIKER / NAMNTRYCK / KUNDSERVICE ================================= */
var win_location = window.location.toString();
$j(function(){
	
	if((win_location.indexOf('trycktekniker') != -1) || (win_location.indexOf('namntryck') != -1) || (win_location.indexOf('inspiration') != -1)){
		$j('.thumb-wrapper img').each(function(i){
			var large_src = $j(this).attr('src').split('small')[0]+'large.jpg';

			if($j(this).attr('longdesc') != undefined) large_src = $j(this).attr('longdesc');

			$j(this).wrap('<a href="#" onclick="switchLargeImg(\''+large_src+'\'); return false"></a>');
		})
	}
	// if anchor in url (kundservice)
	if(win_location.match('#')){
		var win_anchor = win_location.split('#')[1];
		$j(document).find('#'+win_anchor+' div').not('div.kundservice-header, div.slide').each(function(){
			customerServiceExpand($j(this));
		});
	}
})

function switchLargeImg(img_src){
	$j('#zoom_content img:first').attr('src', img_src);
}


