// 
//  js-helper.js
//  cascoag.ch
//  
//  Created by Philippe Manner on 2011-04-13.
//  Copyright 2011 Casco AG. All rights reserved.
// 
			
$(document).ready(function(){
	/* Init navi hover effect */
	$('#navi a.hover').animate({opacity: 0.5}, 0);
	$('#navi a.hover').mouseover(function () { $(this).animate({opacity: 1.0}, 200); });
	$('#navi a.hover').mouseout(function () { $(this).animate({opacity: 0.5}, 200); });
	
	/* PrettyPhoto */
	init_prettyPhoto();
	
	/* Init form validator */
	$("#contact_form").validator({ lang: 'de' });
	$("#contact_form").bind("onFail", function(e, errors)  {
		if (e.originalEvent.type == 'submit') {
			$.each(errors, function()  {
				var input = this.input;
				input.css({borderColor: 'red'}).focus(function()  {
					input.css({borderColor: 'black'});
				});
			});
		}
	});
	
	/* Init form mask */
	$("#contact_form input, #contact_form textarea").focus(function () { $("#contact_fieldset").expose(); } );

	/* Init overlays */
	$("img[rel]").overlay();

});

/* Form validator localisation */
$.tools.validator.localize("de", {
	'*'			: '&larr; Angegebener Wert ist ung&uuml;ltig!',
	':email'  	: '&larr; E-Mail Adresse ist ung&uuml;ltig!',
	':number' 	: '&larr; Kein numerischer Wert!',
	':url' 		: '&larr; URL ist ung&uuml;ltig!',
	'[max]'	 	: '&larr; Zahl muss kleiner als $1 sein!',
	'[min]'		: '&larr; Zahl muss gr&ouml;sser als $1 sein!',
	'[required]': '&larr; Pflichtfeld muss ausgef&uuml;llt werden!'
});


/* Email link generator */
function email(prefix, postfix, superlevel) {
	var mVis = '<span>' + prefix + String.fromCharCode(64) + '</span><span>' + postfix + '.' + superlevel + '</span>';
	var mLnk = email_mailto(prefix, postfix, superlevel);
	document.write('<a href="' + mLnk + '">' + mVis + '</a>');
}

/* Email link generator (just mailto string) */
function email_mailto(prefix, postfix, superlevel) {
	var mLnk = "javascript:document.location.href='mailto:" + prefix + "'+String.fromCharCode(64)+'" + postfix + '.' + superlevel + "';";
	return mLnk;
}

/* Initializes Google map */
function initialize_gmap() {
	var latlng = new google.maps.LatLng(47.181987, 8.465392);
	var myOptions = { zoom: 17, center: latlng, mapTypeId: google.maps.MapTypeId.HYBRID, scrollwheel: false };
	var map = new google.maps.Map(document.getElementById("gmap_contact"), myOptions);
	
	var marker = new google.maps.Marker({
        position: latlng, 
        map: map,
        title:"Hello World!"
    });
    
    var contentString = '<div id="content">'+
        '<div id="siteNotice">Willkommen bei'+
        '</div>'+
        '<h2 id="firstHeading" class="firstHeading">Casco AG</h2>'+
        '<div id="bodyContent">'+
        '<p><b>Alberto Casco</b><br />Zugerstrasse 39<br />6330 Cham<br />Schweiz</p>'+
        '<p><a href="data/Casco_AG.vcf">Visitenkarte</a>, <a href="'+email_mailto('info', 'cascoag', 'ch')+'">E-Mail</a></p>'+
        '</div>'+
        '</div>';
        
    var infowindow = new google.maps.InfoWindow({
        content: contentString
    });
    google.maps.event.addListener(marker, 'click', function() {
      infowindow.open(map,marker);
      marker.show();
    });
}

/* Toggles list item visibility */
function toggleListItem(item) {
	if ($('#item'+item + ' .list_item').is(":hidden")) {
    	$('#item'+item + ' .list_item').slideDown(500);
    	$('#item'+item + ' .list_item_image').css("background-position", "-16px 0" );
    }
    else {
    	$('#item'+item + ' .list_item').slideUp(500);
    	$('#item'+item + ' .list_item_image').css("background-position", "0 0" );
    }
}

/* Sorts the references according to the argument */
function show_references(what) {
	$('.list_item').removeClass('active');
	$('#references_source').quicksand( $('#references_' + what + ' li'), function() {
		init_prettyPhoto();
	});
	$('#list_item_' + what).addClass('active');
}

/* Toggles the contact containers */
function show_contact(what) {
	$('.list_item').removeClass('active');
	$('#list_item_' + what).addClass('active');
	
	$('.contact_box').fadeOut(500);
	$('#box_' + what).delay(500).fadeIn(500);
}

/* Init prettyPhoto */
function init_prettyPhoto() {
	/* Update prettyPhoto gallery IDs */
	$('#references_all li').each(function (i) {
		var rand = parseInt(Math.random() * 1001); // 0 - 1000
		$('#references_source #ref_' + (i+1) + ' a').attr('rel', 'gallery[' + rand + ']');
	});
	
	$("a[rel^='gallery']").prettyPhoto({social_tools:'', slideshow:'3000', deeplinking:false});
}

