$(document).ready(function initialize_page() {

    $('h2.page-ingress-header, h2.footer-header span').css({'-moz-border-radius': '7px', '-webkit-border-radius': '7px'});
    if($('#start').length > 0) {
        start_projector();
    }

	//transform flags from png to gif if browser is IE6
	if($.browser.msie && parseInt($.browser.version) == 6)
	{
		if($("a#language_chooser img").length > 0) {
			var flag_src = $("a#language_chooser img").attr("src");
			var new_flag_src = flag_src.replace(".png",".gif");
			$("a#language_chooser img").attr("src",new_flag_src);
		}
	}
	
	//switch icons when hovering the flags
	$("a#language_chooser img").hover(function() {
		$(this).attr("src",$(this).attr("src").replace("_grey",""));
	},function() {
		$(this).attr("src",$(this).attr("src").replace("flag_se.","flag_se_grey.").replace("flag_en.","flag_en_grey."));
	});

	$("a#language_chooser img").click(function(e){
		e.preventDefault();
		//remember the flag and page url clicked
		var clicked_lang = $(this).attr("id").replace("flag_","");
		var url = window.location;
		//start page falling or fading
		$("#wrap").animate({
		    marginTop: '2000px'
		  }, 500, function() {
			//when page is down, load set-language page in a div to set new lang. then load the new page (with new lang) in #wrap
			$("#masthead").load("/set-language/"+clicked_lang, function() {
				$("#wrap").load(url+" #wrap", function(){
					//reinitialize all event handlers
					initialize_page();
					//when everything is ready, let's drag the new page up
					$("#wrap").animate({
						marginTop: '0px'
					}, 500);
				});
			});
		  });
	});
	
	//styling av blog-bilder
	$("body#blog .entry-content img:not(:.noframe,.wp-smiley)").each(function(){
		var img_width = $(this).width();
		var img_height = $(this).height();
		var img_class = $(this).attr("class");
		$(this).wrap('<div style="width: '+(img_width+10)+'px; height: '+(img_height+10)+'px;" class="blog_pic '+img_class+'" />');
		$(this).before("<span></span>");
		
	});
	
	
	$.localScroll.defaults.axis = 'y';
	// Scroll initially if there's a hash (#something) in the url 
	$.localScroll.hash({
		duration:1500
	});
	$("body#blog .comments-link").localScroll({
		duration:1000,
		hash:true
	});
	//inserting img via javascript to avoid hacking outside the theme folder
	$("#comments").prepend("<img src='/blog/wp-content/themes/socialmonkey/images/comments.png' alt='' /> ");
	
});


function start_projector() {
    
    $('#projector-screen').children().first().addClass('active');
    $('#projector-screen').children().next().hide();
    
    $('#introduction').children().first().addClass('active');
    $('#introduction').children().next().hide();
    
    $('#page-ingress-headers').children().first().addClass('active');
    
    
    window.setInterval(function() {
        change_slide('introduction', 'page-ingress-headers', 'projector-screen');
    }, 5000);
    
}


function change_slide(text_parent_id, header_parent_id, screen_parent_id) {

    var current_text = $('#'+text_parent_id).children(".active");
    var current_header = $('#'+header_parent_id).children(".active");
    var current_screen = $('#'+screen_parent_id).children(".active");
    
    var text_offset = current_text.offset();
    current_text.fadeOut(1000).removeClass('active').next().fadeIn(1000).addClass('active');
    
    var screen_offset = current_screen.offset();
    current_screen.fadeOut(1000).removeClass('active').next().fadeIn(1000).addClass('active');
    
    current_header.removeClass('active').next().addClass('active');
    
    if(current_header.is(":last-child")) {
        $('#'+screen_parent_id).children().first().fadeIn(1000).addClass('active');
        $('#'+text_parent_id).children().first().fadeIn(1000).addClass('active');
        $('#'+header_parent_id).children().first().addClass('active');
    }

}
