
blocket("@init.index.self").extend(function() {
	// Mouseover map
	$(".regions a").add("#sweden_3d area").mouseover(function() {
		$("#mouseover").removeClass().addClass("sprite_index_mouseover_region_" + this.id.substring(this.id.indexOf("_") + 1));	
		$(".region_link_list").removeClass("underlined");
		var id = this.id.substring(this.id.indexOf("_") + 1);
		$("#area_"+id).addClass("underlined");
	});

	$(".regions a").add("#sweden_3d area").mouseout(function() {
		$("#mouseover").removeClass();
	});

	$(".regions a,#sweden_3d area").click(function() {
		var ca = $(this).attr("href");
		ca = ca.split("ca=")[1];
		$.cookie('default_ca', ca, {"expires": 365, "path": '/', "domain": footer_gallery.cookie_domain}); //We shouldn't have to use footer_gallery for this value
	});


	//take over surrounding clickspace for IE7

	if($(".ie_7").length >0 ){
		if($(".big_bang_viasat").length > 0){
			var body_mouseover = 0;
			var blocket_mouseover = 0;
			$("body").css("cursor","pointer");
		
			$("#blocket").mouseover(function() {
				blocket_mouseover = 1;
				$("body").css("cursor","auto");
			});
			$("#blocket").mouseout(function() {
				blocket_mouseover = 0;
				$("body").css("cursor","pointer");
			});		
			$(".big_bang_viasat").click(function() {
				if(blocket_mouseover == 1){
					return;
				}else{
					var go_link = $(".big_bang_clickspace_surrounding_right a").attr("href");
					window.open(go_link);
				}
			});	
		}	

	}


	blocket.apps.index.setup_lightboxes();
	blocket.apps.index.remove_scroll();

	// Truncate news if they're too long
	blocket.apps.news.truncate();

	$('#xiti_estore').click(function(event) {
		event.preventDefault();
		window.location.href = 'http://logc89.xiti.com/go.url?xts=459270&xtor=AD-4-[ad_version1]-[text_link]-[0]-[www.blocket.se]-[0]-[ecommerce_from_index_regions]&url=' + this.href;
	});
	
	jQuery(".lightbox_element").click(function (){
		blocket.apps.lightbox.open({type : "url", data: jQuery(this).attr("href")});
		return false;
	});

	//Delete old footer gallery cookies
	// domain "" sets the domain to www.blocket.se
	$.cookie("footer_gallery_visited","",{path : "/", domain : "", expires : -1});


	$("body").bind("click", function(e){
	  if ($(e.target).is(".mediamarkt")) {
		 window.open(blocket.params.takeover_lnk, "_blank");
	  }
	});
});

blocket("@apps.index").extend({
	setup_lightboxes : function() {
		/* Setup lightbox functionality for regions with sub areas */
		$("#area_1, #maparea_1").click(function() {
			blocket("#"+$(this).attr("id")).apps.lightbox.open({
				type : "iframe",
				settings : {
					width: "390px",
					height: "460px",
					name: "nbl_iframe"
				}
			});
			return false;
		});

		/* Setup lightbox functionality for regions with sub areas */
		$("#area_23, #maparea_23").click(function() {
			blocket("#"+$(this).attr("id")).apps.lightbox.open({
				type : "iframe",
				settings : {
					width: "420px",
					height: "455px",
					name: "skl_iframe"
				}
			});
			return false;
		});
	},

	/* Remove vertical scroll for window-width larger than site-content */
	remove_scroll : function() {
		if($(window).width() > $("#blocket").width())
			$("html").addClass("x_hidden");
	},

	/* Change area image onmouseover on index page */
	highlight_map : function(region, out) {
		var LinkItem = document.getElementById("area_" + region);
		var HighlightItem = document.getElementById("area_highlight");

		if (!out) {
			LinkItem.style.textDecoration = "underline";
			$("#area_"+region).css('background','#111');
			//HighlightItem.style.backgroundPosition = 354 + (-354 * region) + 'px 0px';
		} else {
			LinkItem.style.textDecoration = "none";
			HighlightItem.style.backgroundPosition = '354px 0px';
		}

		return true;
	},

	// Firefox startpage info box 
	startpage_ff : function() {
		document.getElementById('startpage_ff').style.display = 'none'; 
		document.getElementById('startpage_ff_info').style.display = 'block'; 		
		this.startpage_set(); 
	},

	// See if startpage-icon cookie is set 
	startpage_cookie_get : function() {
		var cookies = document.cookie.split("; ");	
		
		for(i = 0; i < cookies.length; i++) {
			if(cookies[i] == "b_sp=1") 
				return true;
		}	

		return false;
	},

	/* Register click in cookie and add statistics */ 
	startpage_set : function() {
		/* Set cookie */
		var cookie_expire = new Date();                                
		cookie_expire.setTime(cookie_expire.getTime() + 1000*60*60*24*365);
		cookie_expire=cookie_expire.toGMTString();
		document.cookie="b_sp=1; expires="+cookie_expire+";";

		/* Ajax */
		blocket.common.ajax_request("/redir?s=startpage_click&nc=1", null, blocket.apps.index.startpage_callback, null, true, "GET"); 

		/* Hide icon */
		document.getElementById("startpage_ie").style.display = 'none'; 
	},

	/* Dummy callback */
	startpage_callback : function(result, xmlhttp, link) {
	},

	/* Hide FF-info box */
	startpage_ff_info_close : function() {
		document.getElementById("startpage_ff_info").style.display = 'none';
	},

	/* Save default ca */
	startpage_set_default_ca : function(ca, domain) {
		blocket.common.setCookie('default_ca', ca, 365, '/', domain, false);

		return true;
	}
});

