/********************************************************************
* Project: Accurate
* File: scripts.js
* Directory: js/
* Handles: Javascript functions
* Produced by: AdaptiveMedia
* Start date: 2010-07-07
********************************************************************/
$(function(){
						   
	
	var root = $("body").attr("alt");
	
	var loadingAnimation = '<div align="center"><img src="' + root + 'img/loading.gif" /></div>';	
	
	/**********************
	* Set footer height
	**********************/
	var height = screen.height;
	if(height > 1080) {
		$("#footer").css("min-height", 400);
	}
	
	/*****************************
	*
	* Generic common functions
	* 
	*****************************/
	
	// Mark all checkboxes
	$(".mark_all_checkboxes").click(function(){
		var target = $(this).attr("value");
		
		$("." + target).attr("checked", true);
		
		return false;
	});
	
	// Unmark all checkboxes
	$(".unmark_all_checkboxes").click(function(){
		var target = $(this).attr("value");
		
		$("." + target).attr("checked", false);
		
		return false;
	});

	// Button as anchor
	$(".button_link").click(function(){
		var href = $(this).attr("href");
		
		window.location = href;
	});
	
	// Disable anchor or somthing else
	$(".disabled").click(function() {
		return false;
	});
	
	// Empty field on focus 
	$(".on_focus_empty").click(function(){
		var alt = $(this).attr("alt");
		if($(this).val() == alt) 
			$(this).val('');
		
	});
	
	$(".on_focus_empty").focus(function(){
		$(this).select();
	});
	
	// Set focus to field
	$(".set_focus").click(function(){
		var alt = $(this).attr("alt");
		$("input[name='" + alt + "']").addClass("red_border");
		$("input[name='" + alt + "']").focus();
		
		return false;
	});
	
	// Switch between tabs 
	$(".tab_switcher").click(function(){
		var href = $(this).attr("href");
		var parts = href.split("#");
		var target = parts[1];
		
		var target_element = $("#" + target);
		var hide_elements_class = $(this).attr("alt");
		
		//remove current
		$(".tab_switcher").removeClass("tab_switcher_current");
		
		//add class for just this
		$(this).addClass("tab_switcher_current");
		
		// show and hide
		$("." + hide_elements_class).hide();
		target_element.show();
		
		return false;
	});
	
	// Switch between divs
	$(".checkout_div_switcher").click(function(){
		var href = $(this).attr("href");
		var parts = href.split("#");
		var target = parts[1];
		
		var target_element = $("#" + target);
		var hide_elements_class = $(this).attr("alt");
				
		// show and hide
		$("." + hide_elements_class).hide();
		target_element.show();
		$(".checkout_tabs a").removeClass("current"); 
		$("#" + target + "_tab").addClass("current");
		$("#" + target + "_tab").removeClass("disabled");

		
		return false;
	});
	
	// Toggle an elements visibility
	$(".toggle_element").click(function(){

		$("#" + $(this).attr("alt")).toggle('fast');
		
		return false;
										
	});
	
	// Hide this
	$(".hide_this").click(function(){
		$(this).hide();
	});
	
	// Show popup confirm box
	$(".confirm_box, .confirm").click(function(){
		var string = $(this).attr("title");
		
		if( ! string)
			string = "Är du säker?";
			
		if(confirm(string))
			return true;
		return false;
	 });
	
	// Show confirm box with URL as destination
	$(".confirm_goto_url").click(function(){
		var string = $(this).attr("title");

		if( ! string)
			string = "Är du säker?";
			
		if(confirm(string))
			window.location = $(this).attr("href");
			
		return false;
	 });
	 
	 // Enable an element
	 $(".enable_element").click(function(){
	 	var alt = $(this).attr("alt");

		$("#" + alt).attr("disabled", false);
	 });
	 
	 // Shows or hides a div
	 $(".show_hide_div").click(function(){
	 	var alt = $(this).attr("alt");
		var href = $(this).attr("href");
		var parts = href.split("#");
		var target = parts[1];
		var target_div = $("#" + target);
		
		if(alt == 'show' && target_div.hasClass("hidden")) {
			target_div.removeClass("hidden");
		}
		else if(alt == 'hide') {
			if( ! target_div.hasClass("hidden")) {
				target_div.addClass("hidden");
			}
		}
		
	 });
	
	
	// Set a text value 
	$(".set_input_value").click(function(){
		var id = $(this).attr("id");
		var alt = $(this).attr("alt");

		$("input[name='" + id + "']").val(alt);
	});					  
	
	
	
	/********/
	
	// check contact form
	$("#contact_email").click(function(){
		var name = $(".contact_form input[name='fromName']").val();
		var email = $(".contact_form input[name='email']").val();
		var message = $(".contact_form textarea[name='message']").val();
		
		if(!name || !email || !message) {
			alert("Du måste fylla i alla fälten");
			return false;
		}
	});
	
	// TOOLTIP
	xOffset = 10;
	yOffset = 20;					
	$(".tooltip").hover(function(e){											  
		this.t = this.title;
		this.title = "";									  
		$("body").append("<p id='tooltip'>"+ this.t +"</p>");
		$("#tooltip")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px")
			.fadeIn("fast");		
    },
	function(){
		this.title = this.t;		
		$("#tooltip").remove();
    });	
	$(".tooltip").mousemove(function(e){
		$("#tooltip")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px");
	});
	$(".tooltip_link").hover(function(e){											  
		this.t = this.title;
		this.title = "";									  
		$("body").append("<p id='tooltip_link'>"+ this.t +"</p>");
		$("#tooltip_link")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px")
			.fadeIn("fast");		
    },
	function(){
		this.title = this.t;		
		$("#tooltip_link").remove();
    });	
	$(".tooltip_link").mousemove(function(e){
		$("#tooltip_link")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px");
	});
	
	


	
	// Make objects draggable
	for(var i=1;i<=9;i++) {
		
		//$("#draggable_pub" + i).draggable();
		
	}
	$(".draggable").draggable();
	
	// prevent newsletter spam
	$("#submit_newsletter_form").submit(function(){
		var email = $(this).find("input[name='email']").val();
		if( ! is_email(email) || email.length < 5 || email == '') {
			alert("Skriv in en korrekt emailadress");
			return false;
		}
	});
	
	//
	// events for product menu
	//
	var prod_menu_container = $(".product_menu");
	var prod_menu_li = $(".product_menu li");
	var prod_menu_li_ul = $(".product_menu li ul.submenu");
	
	// expand submenu
	$(".product_menu_wrapper .product_menu li img.expand").click(function(){
																		  
		var id = $(this).attr("id");
													
		// hide all always
		//$(".product_menu ul.submenu").slideUp('fast');
		$(".product_menu ul.submenu").slideUp('fast');
		$(".product_menu li img.current").attr("src", root + "img/toggle_small_expand2.png");
		
		// if this has class
		if($(this).hasClass("current")) {
			$(this).removeClass("current");
			
			// change image
			$(this).attr("src", root + "img/toggle_small_expand2.png");
		}
		else {
			$(this).addClass("current");
			$("#submenu_" + id).slideDown('fast');
			// change image
			$(this).attr("src", root + "img/toggle_small2.png");
		}
		

	});
	
	
	function is_email(str) {
		return (str.lastIndexOf(".") > 2) && (str.indexOf("@") > 0) && (str.lastIndexOf(".") > (str.indexOf("@")+1));
	}

	
	
});
