/* Fill formfields with temp data */
var formFiller = {
	fill: function(fieldId, fieldValue){
		if($('#'+fieldId).length == 0) return;

        if($('#'+fieldId).val() == ""){
            $('#'+fieldId).val(fieldValue);
        }else{
            fieldValue = $('#'+fieldId).val();
        }
        
		$('#'+fieldId).focus(function () {
			if($('#'+fieldId).val() == fieldValue){
				$('#'+fieldId).val('');
			}
		});

		$('#'+fieldId).blur(function () {
			if($('#'+fieldId).val() == ''){
				$('#'+fieldId).val(fieldValue);
			}
		});
	}
}

var contact = {
    allCountries: null,
    activeCountry: null,
    countryTitle: null,

    init:function(){
		
        if($(".contact_data, #newsletter").length == 0) return;
		
        // finds all countries in selectbox (links with classnames) and init events
        contact.allCountries = $("#countries li").find("a");
        $(contact.allCountries).each(function(){
            $(this).click(function(ev){
                contact.activeCountry = $(this).attr('class');
                contact.activateCountry();
				$('#maps_address').val($(this).attr('location'));
				$('#maps_language').val($(this).attr('language'));
                ev.preventDefault();
            });
        });

        $(".contact_map_overlay a").each(function(){
            $(this).click(function(ev){
                contact.activeCountry = $(this).attr('class');
				$('#maps_address').val($(this).attr('location'));
				$('#maps_language').val($(this).attr('language'));
                contact.activateCountry();
                ev.preventDefault();
            });
        });

		$('#get_directions_form').submit(function(ev) {
			window.open(
				'http://maps.google.' + $('#maps_language').val() + '/?q='
					+ 'from:' + $('#directions_input').val()
					+ ' to:'  + $('#maps_address').val()
			);
			ev.preventDefault();
		});

        // first time there is no country selected, so we need to find the first one and activate it
        if(contact.activeCountry == null){
            contact.activeCountry = $(contact.allCountries[0]).attr('class');
            contact.activateCountry();
        }
    },
    activateCountry:function(){
       contact.showCountryData();
       contact.showCountryLabel();
       contact.setSelectboxText();
    },
    showCountryData: function(){
        $(".countries_data p").css('display', 'none');
        $(".countries_data p."+contact.activeCountry).css('display', 'block');
    },
    showCountryLabel: function(){
        $(".contact_map_overlay a img").hide();
        $(".contact_map_overlay a."+contact.activeCountry+" img").fadeIn('medium');
    },
    setSelectboxText: function(){
        $(".select span.text").html("");
        $(".select span.text").append($("#countries li a."+contact.activeCountry).html());
    }
}

var newsletter = {
    init:function(){
		var splitUrl = document.URL.split("/");
		var lastUrlItem = splitUrl[splitUrl.length-1];

		//init state
		if (lastUrlItem == "" | lastUrlItem == "newsletter") {
			$("#subscribe_form").css("display","block");
			$("input#subscribe").attr("checked", "checked");
		} else if (lastUrlItem != "subscribed" /* && lastUrlItem != "unsubscribed" */ ) {
			$("#unsubscribe_form").css("display","block");
			$("input#unsubscribe").attr("checked", "checked");
			if (lastUrlItem != "unsubscribed") {
				$("#email_unsubscribe").val(lastUrlItem);
			}
		}

		//animate subscriber view
		 $("input#subscribe").click(function(){
			 $("#unsubscribe_form").slideUp(500,function(){
				$("#subscribe_form").slideDown(500, function(){
					$("ul.countries").fadeIn("fast");
				});
			 });
		 })

		//animate unsubscriber view
		 $("input#unsubscribe").click(function(){
			 $("ul.countries").fadeOut("fast");
			 $("#subscribe_form").slideUp(1000,function(){
				$("#unsubscribe_form").slideDown(500);
			 });
		 })

		//select a country
		 $("ul.countries ul.selector li a").click(function(){

			 $("ul.countries span.text").html( $(this).html() );
			$("input#country_id").val( $(this).attr("class") );

			$("ul.countries ul.selector").css("display","none");
			$("ul.countries span.arrow").css("{background-position","bottom left");

			 return false;
		 })

		 //defaults
		  $("ul.countries a.select").click(function(){
			  return false;
		  })

		//button submits and validation
		$('#submitSubscribe').click(function(e) {
			var currentForm = $('#subscribeForm');
			var foundError = 0;

			currentForm.find("input.required").each(function(i){
				if($(this).val() == ""){foundError = 1};
			})
			if (foundError == 1) {
				currentForm.find("div.error_message").show();
				$("#newsletter .bd .countries ").css("top","260px");
				return false;
			} else {
				currentForm.find("div.error_message").hide();
				$("#newsletter .bd .countries ").css("top","229px");
				currentForm.submit();
			}
			
			e.preventDefault();
		});

		$('#submitUnsubscribe').click(function(e) {
			var currentForm = $('#unsubscribeForm');
			var foundError = 0;

			currentForm.find("input.required").each(function(i){
				if($(this).val() == ""){foundError = 1};
			})
			if (foundError == 1) {
				currentForm.find("div.error_message").show();
				return false;
			} else {
				currentForm.find("div.error_message").hide();
				currentForm.submit();
			}

			e.preventDefault();
		});



	}
}

$(document).ready(function(){
    formFiller.fill("txt_search_input", "type keyword");
    formFiller.fill("directions_input", "Get directions from...");
    contact.init();
	newsletter.init();
    if($(".right-dotted-border").length > 0){
        var maxHeight = 0;
        $(".right-dotted-border").each(function(){
            if(maxHeight <= $(this).height()) maxHeight = $(this).height();
        });
        $(".right-dotted-border").height(maxHeight);
    }
});


