$(document).ready(function() {
	
	$("a.popup").click(function() { 
		window.open(this.href,this.href,'width=550,height=600,scrollbars=1,resizable=1');
		return false; 
	});
	
	$(".shop .fields .radio input").click(function() {
		$(".shop .fields .itemselected").attr("class","item");
        if($(this).is(":checked")) {
            $(this).parent().parent().attr("class","itemselected");
        }
	});
	
	$('#showTools').click( function() {									
		if($(this).attr("class") == "button_exexp") {
			$(this).attr("class", "button_excol");
		} else {
			$(this).attr("class", "button_exexp");
			$(this).parent().attr("class", "headRowLine");
		}		
    $('#tools').toggle();
	});
	
	$('#showPratic').click( function() {
		if($(this).attr("class") == "button_exexp") {
			$(this).attr("class", "button_excol");
			
			} else {
			$(this).attr("class", "button_exexp");
			$(this).parent().attr("class", "headRowLine");
		}		
    	$('#pratic').toggle();

	});
	
	$('#showFaq').click( function() {
		if($(this).attr("class") == "button_exexp") {
			$(this).attr("class", "button_excol");
			} else {
			$(this).attr("class", "button_exexp");
		}	
    	$('#faq').toggle();
	});
	
	$('#showApropos').click( function() {
		if($(this).attr("class") == "button_exexp") {
			$(this).attr("class", "button_excol");
			} else {
			$(this).attr("class", "button_exexp");
		}	
    	$('#apropos').toggle();
	});
	
	$(".header .main_navigation_right .closed .webmail").click(function() {
		$(".header .main_navigation_right .closed").hide();
		$(".header .main_navigation_right .open").show();
	});

	$(".header .main_navigation_right .open .webmail").click(function() {
		$(".header .main_navigation_right .open").hide();
		$(".header .main_navigation_right .closed").show();
	});

	$(".header .main_navigation_right .open .inputs .password").focus(function() {
		$(this).attr("type","password").attr("value","");
	});

	$(".radiobox_options .select_button").click(function() {
		$(".radiobox_options .option_selected").attr("class","option");
        if($(this).is(":checked")) {
            $(this).parent().parent().attr("class","option_selected");
        }
	});
	
	$(".shop .shop_box .subscr_compare .selects input").click(function() {
		var col = $(this).attr("class");
		$(".shop .shop_box .subscr_compare table tbody .col").removeClass("active");
		$(".shop .shop_box .subscr_compare table tbody ." + col).addClass("active");
	});
	
	$(".shop .shop_box .subscr_compare .item_row").hover(function() {
		$(this).addClass("row_hover");
	}, function() {
		$(this).removeClass("row_hover");
	});
	
	$(".shop .shop_box .box_header a").click(function() {
		if($(this).is(".shop_expand")) {
			$(this).parent().parent().find(".box_contents").slideDown(200, function() {
				var set_back = $(this).parent().find(".shop_collapse").text();
				$(this).parent().find(".shop_collapse").text($(this).parent().find(".switch").text());
				$(this).parent().find(".switch").text(set_back);
			});
			$(this).attr("class","shop_collapse");
		}
		else if($(this).is(".shop_collapse")) {
			$(this).parent().parent().find(".box_contents").slideUp(200,function() {
				$(this).parent().find(".shop_collapse").attr("class","shop_expand");
				var set_back = $(this).parent().find(".shop_expand").text();
				$(this).parent().find(".shop_expand").text($(this).parent().find(".switch").text());
				$(this).parent().find(".switch").text(set_back);
			});
		}
	});
	$(".shop .shop_box_inline .box_inline_header a").click(function() {
		if($(this).is(".shop_inline_expand")) {
			$(this).parent().parent().find(".box_inline_contents .content_bottom").slideDown(200, function() {
				$(this).parent().parent().removeClass("collapsed");
				$(this).parent().parent().addClass("expanded");
			});
			$(this).attr("class","shop_inline_collapse");
		}
		else if($(this).is(".shop_inline_collapse")) {
			$(this).parent().parent().find(".box_inline_contents .content_bottom").slideUp(200, function() {
				$(this).parent().parent().removeClass("expanded");
				$(this).parent().parent().addClass("collapsed");
			});
			$(this).attr("class","shop_inline_expand");
		}
	});
	
	$(".main .generic .tabs ul li a").click(function() {
		var view_this = $(this).attr("class");
		$(this).parent().parent().parent().parent().find(".tab_wrapper").hide();
		$(this).parent().parent().parent().parent().find(".tabs ul li").removeClass("selected");
		$(this).parent().parent().parent().parent().find(".wrapper_" + view_this).show().addClass("active");
		$(this).parent().addClass("selected");
	});
	
	// Static function to change image on hover for item-icons in shopingflow.
	// Should be rebuilt to function completely  dynamic.
	$(".shop .row .item_specs img").hover(function() {
		var set_to = "img/icon_" + $(this).attr("class") + "_phone_hover.gif";
		$(this).attr("src",set_to);
		$(this).parent().parent().find(".item_info").text($(this).parent().find(".icon_info").html());
	}, function() {
		var set_to = "img/icon_" + $(this).attr("class") + "_phone.gif";
		$(this).attr("src",set_to);
		$(this).parent().parent().find(".item_info").text("");
	});

	$(".webmail_main .webmail_header .menu a:not(.active) img").hover(function() {
		var set_to = $(this).attr("src");
		set_to = set_to.substr(0,set_to.length-4) + "_hover.gif";
		$(this).attr("src",set_to);
	}, function() {
		var set_to = $(this).attr("src");
		set_to = set_to.substr(0,set_to.length-10) + ".gif";
		$(this).attr("src",set_to);
	});
	
	$(".main .main_left .header ul li a img").hover(function() {
		var set_to = $(this).attr("src");
		set_to = set_to.substr(0,set_to.length-4) + "_h.gif";
		$(this).attr("src",set_to);
	}, function() {
		var set_to = $(this).attr("src");
		set_to = set_to.substr(0,set_to.length-6) + ".gif";
		$(this).attr("src",set_to);
	});
	
	$(".webmail_main .webmail_header .navigation input.mail_search").focus(function() {
		$(this).attr("value","");
	});

	// Meny-hovering
	
	$("#sub_menu .item_mid").hover( function() {
		$(this).addClass("item_mid_hover");
	}, function() {
		$(this).removeClass("item_mid_hover");
	});
	
	$("#sub_menu .item_sub").hover(function() {
		$(this).addClass("item_sub_hover");
	}, function() {
		$(this).removeClass("item_sub_hover");
	});
	
	$("#sub_menu .item_first_selected").hover(function() {
		$(this).addClass("item_first_selected_hover");
	}, function() {
		$(this).removeClass("item_first_selected_hover");
	});
	
	$("#sub_menu .open").hover(function() {
		$(this).addClass("open_hover");
	}, function() {
		$(this).removeClass("open_hover");
	});
	
	$("#sub_menu .item_first").hover(function() {
		$(this).addClass("item_first_hover");
	}, function() {
		$(this).removeClass("item_first_hover");
	});
	
	$("#sub_menu .item_sub_last_selected").hover(function() {
		$(this).addClass("item_sub_last_selected_hover");
	}, function() {
		$(this).removeClass("item_sub_last_selected_hover");
	});
	
	$("#sub_menu .item_open").hover(function() {
		$(this).addClass("item_open_hover");
	}, function() {
		$(this).removeClass("item_open_hover");
	});
	$("#searchfield").click(function() {
		$(this).attr("value","")
	});
	
	$("#broadbandsearch").click(function() {
		$(this).attr("value", "");
	});
	/* $("#broadbandsearch").change(function() {
		if($(this).attr("value").length == 0 )  { 
			$(this).attr("value", "yes") 
		}
	}); */
	
	$("#clickbox .box_content").click(function() {
		document.location.href = $(this).find(".text a").attr("href");
	})
	
		/* popup link */
	$('.jPopup').click(function(){												
		var s = getParam(this.href, 's') || 1;
		var w = getParam(this.href, 'w') || 600;
		var h = getParam(this.href, 'h') || 400;
		var l = ($(window).width())/2 - w/2;
		var t = ($(window).height())/2 - h/2;
		window.open(this.href, '','top='+t+', left='+l+', width='+w+',height='+h+', scrollbars='+s+', resizable=1');
		return false;
	});
});

/* parsing of url parameters */
function getParam(url, name){
	var name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	var regexS = "[\\?&]"+name+"=([^&#]*)";
	var regex = new RegExp( regexS );
	var results = regex.exec( url );
	return results == null ? "" : results[1];
}
