function goToForm(theForm, theSubject) {
	document.location.href = theForm + ".html?r=" + theSubject;
}

// return the index of the valueToFind of the given selectBox (property means "text" or in "value" of the select)
function findValue(selectBox,valueToFind,property) {
  for (var i = 0; i < selectBox.options.length; i++) {
    if (selectBox.options[i][property].toUpperCase().indexOf(valueToFind.toUpperCase()) == 0) {
      return i;
    }
  }
}

$(document).ready(function() {
			$('#sub_menu a[@href^="http"]').click(function(){
				this.target = "_blank";
			});
													 
		$('.jTip').hover(function(){
			$(this).next().css('opacity', '0.9').fadeIn();
		}, function(){
			$(this).next().hide();
		});  

													 
		$.fn.search = function(type) {
		return this.focus(function() {
			if( this.value == this.defaultValue ) {
				this.value = "";
			}
		}).blur(function() {
			if( !this.value.length ) {
				this.value = this.defaultValue;
			}
		});
	};
	$('.jClean').search();
													 
	$('a.enlarge').next().css('display','none');											 
	$('a.enlarge').click( function() {
			if ($(this).next().is(":hidden")) {
				$('a.enlarge').removeClass("enlarge_selected");
				$(this).addClass("enlarge_selected");
				$('a.enlarge').next().slideUp("fast");
        $(this).next().slideDown("fast");
      } else {
				$(this).removeClass("enlarge_selected");
        $(this).next().slideUp("fast");
      }
	});
	
	
  var menuH = $('#sub_menu').height() + 130;
	var genericH = $('.generic').height();		// Generic templates
	var csH = $('.cs').height();				// Costumer support
	var contactH = $('.contact').height();		// Contact support
	var mobilefunH = $('.mobilefun').height();	// Mobile fun

	if (menuH > genericH) { $('.generic').css("height", menuH); $('.generic').css("min-height", menuH); }
	if (menuH > csH) { $('.cs').css("height", menuH); $('.cs').css("min-height", menuH); }
	if (menuH > contactH) { $('.contact').css("height", menuH); $('.contact').css("min-height", menuH); }
	if (menuH > mobilefunH) { $('.mobilefun').css("height", menuH); $('.mobilefun').css("min-height", menuH); }

	/****
	$("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();

	});

	$('#showSupport').click( function() {
		if($(this).attr("class") == "button_exexp") {
			$(this).attr("class", "button_excol");
			
			} else {
			$(this).attr("class", "button_exexp");
			$(this).parent().attr("class", "headRowLine");
		}		
    	$('#support').toggle();

	});	
	
	$('#showPromo').click( function() {
		if($(this).attr("class") == "button_exexp") {
			$(this).attr("class", "button_excol");
			
			} else {
			$(this).attr("class", "button_exexp");
			$(this).parent().attr("class", "headRowLine");
		}		
    	$('#promo').toggle();

	});	

	$('#showAreaclienti').click( function() {
		if($(this).attr("class") == "button_exexp") {
			$(this).attr("class", "button_excol");
			
			} else {
			$(this).attr("class", "button_exexp");
			$(this).parent().attr("class", "headRowLine");
		}		
    	$('#areaclienti').toggle();

	});	
	
	$('#showTutteLeOfferte').click( function() {
		if($(this).attr("class") == "button_exexp") {
			$(this).attr("class", "button_excol");
			
			} else {
			$(this).attr("class", "button_exexp");
			$(this).parent().attr("class", "headRowLine");
		}		
    	$('#tutteLeOfferte').toggle();

	});	
	
	$('#showSpecialeOnline').click( function() {
		if($(this).attr("class") == "button_exexp") {
			$(this).attr("class", "button_excol");
			
			} else {
			$(this).attr("class", "button_exexp");
			$(this).parent().attr("class", "headRowLine");
		}		
    	$('#specialeOnline').toggle();

	});
	
	/*
	$('#showFaq').click( function() {
		if($(this).attr("class") == "button_exexp") {
			$(this).attr("class", "button_excol");
			} else {
			$(this).attr("class", "button_exexp");
		}	
    	$('#faq').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:not(.active) 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").hover(function() {
		$(this).addClass("item_sub_last_hover");
	}, function() {
		$(this).removeClass("item_sub_last_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");
	});
	$(".promo_quotes").hover(function() {
		$(".promo_quotes .navi_bottom .forward_arrow").hover(function() {
			$(this).attr("src","../b2b/img/navi_button_right_h.gif");
		}, function() {
			$(this).attr("src","../b2b/img/navi_button_right_box_h.gif");
		});
		$(".promo_quotes .navi_bottom .back_arrow").hover(function() {
			$(this).attr("src","../b2b/img/navi_button_left_h.gif");
		}, function() {
			$(this).attr("src","../b2b/img/navi_button_left_box_h.gif");
		});
		$(".promo_quotes .navi_bottom .forward_arrow").attr("src","../b2b/img/navi_button_right_box_h.gif");
		$(".promo_quotes .navi_bottom .back_arrow").attr("src","../b2b/img/navi_button_left_box_h.gif");
	}, function() {
		$(".promo_quotes .navi_bottom .forward_arrow").attr("src","../b2b/img/navibutton_gray_right.gif");
		$(".promo_quotes .navi_bottom .back_arrow").attr("src","../b2b/img/navibutton_gray_left.gif");		
	});
	
	$(".glider .forward_button .jcarousel-next .next_img").hover(function() {
		$(this).attr("src","../b2b/img/sb_landing_news_rightarrow_h.gif");
	}, function() {
		$(".glider .forward_button .jcarousel-next .next_img").attr("src","../b2b/img/sb_landing_news_rightarrow.gif");
	});
	
	$(".glider .back_button .jcarousel-prev .previous_img").hover(function() {
		$(this).attr("src","../b2b/img/sb_landing_news_leftarrow_h.gif");
	}, function() {
		$(".glider .back_button .jcarousel-prev .previous_img").attr("src","../b2b/img/sb_landing_news_leftarrow.gif");
	});
	
	var glider = $(".glider");
	if (glider.size() > 0) {
		var speed = null;
		$(".glider").each(function() {
			if($(this).parent().attr("class") == "news_glide") {
				speed = 6;
			} else {
				speed = 8;
			}
		    $(this).jcarousel({
		        wrap: true,
				autoScroll: speed,
				itemVisible:1,
				itemScroll: 1		
		    });
		});
	}
	if(document.getElementById('slideshow')) {
		$('#slideshow').slideshow({
					timeout: 2000,
					type: 'random',
					pauselink: 'pause1',
					pausecallback: function(self){
						self.html('Play')
					},
					playcallback: function(self){
						self.html('Pause');
					}
			
		});
	}
	$('#showBusiness').click( function(){
		if($('#business').attr("class") == "isnotExp"){
			$('#business').slideDown("fast", function(){
				$('#business').attr("class", "isExp");
			});
		}
	});
	$(document).click( function(){
		if($('#business').attr("class") == "isExp"){
			$('#business').slideUp("fast", function(){
				$('#business').attr("class", "isnotExp");
			});
		}
	});
	$('#clear_form').click( function(){
		$('.contact input').attr("value", "");
	});
	
	
	
	$('.button_orange').hover(function(){
		$(this).addClass("button_red");
		$(this).removeClass("button_orange");
		}, function() {
		$(this).addClass("button_orange");
		$(this).removeClass("button_red");	
	});
	$('.button_green').hover(function(){
		$(this).attr("class", "button_green_hover");
		}, function() {
		$(this).attr("class", "button_green");
	});
});


	


function popWindow(url, sizeX, sizeY) {
	return window.open(url, "Tele2", "width=" + sizeX + ",height=" + sizeY + ",status=no,toolbar=no,menubar=no,location=no,scrollbars=0");
}

/* slideshow plugin */

jQuery.fn.slideshow = function(options) {
	var settings = {
		timeout: '2000',
		type: 'sequence',
		pauselink: null,
		playcallback: null,
		pausecallback: null
	}
	if(options)
		jQuery.extend(settings, options);
	
	var pauseState = 0;
	var current = 1;
	var last = 0;
	var timer = '';
	
	var change = function () {
		if ( pauseState == 0 ) {
			for (var i = 0; i < slides.length; i++) {
				jQuery(slides[i]).css('display', 'none');
			}
			jQuery(slides[last]).css('display', 'block').css('zIndex', '0');
			jQuery(slides[current]).css('zIndex', '1').fadeIn('slow');
			
			if ( settings.type == 'sequence' ) {
				if ( ( current + 1 ) < slides.length ) {
					current = current + 1;
					last = current - 1;
				}
				else {
					current = 0;
					last = slides.length - 1;
				}
			}
			else if ( settings.type == 'random' ) {
				last = current;
				while (	current == last ) {
					current = Math.floor ( Math.random ( ) * ( slides.length ) );
				}
			}
			else {
				alert('type must either be \'sequence\' or \'random\'');
			}
			timer = setTimeout(change, settings.timeout);
		}
	}

	var pause = function() {
		if ( pauseState == 0 ) {
			pauseState = 1;
			clearTimeout(timer);
			if ( settings.playcallback != null ) {
				settings.pausecallback(jQuery('#' + settings.pauselink));
			}
		}
		else {
			pauseState = 0;
			change();
			if ( settings.playcallback != null ) {
				settings.playcallback(jQuery('#' + settings.pauselink));
			}
		}
	}
	
	this.css('position', 'relative');
	var slides = this.find('img').get();
	jQuery.each(slides, function(i){
		jQuery(slides[i]).css('zIndex', slides.length - i).css('position', 'absolute').css('top', '0').css('left', '0');
	});
	if ( settings.type == 'sequence' ) {
		timer = setTimeout(change, settings.timeout);
	}
	else if ( settings.type == 'random' ) {
		do { current = Math.floor ( Math.random ( ) * ( slides.length ) ); } while ( current == 0 )
		timer = setTimeout(change, settings.timeout);
	}
	else {
		alert('type must either be \'sequence\' or \'random\'');
	}
	
	if ( settings.pauselink != null ) {
		jQuery('#' + settings.pauselink).click(pause);
	}
	return this;
};