/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

function unflip()
{
    $("div#card-back").fadeOut("fast",function(){
        $("div#card-container").animate({
            height: cardHeightBeforeFlip
        }, 300, function(){
            $("div#card-container").flip({
                direction:'bt',
                color: '#333',
                onBefore: function(){

                },
                onAnimation: function(){
                    //$("div#card-container").height(200);
                },
                onEnd: function(){
                    $("ul.vcard").fadeIn();
                    $("div#card-container").height("auto");
                }
            });
        });
    });
}

function cardFlip(el, href, func)
{
    if(func == undefined) func = false;
    $.get(href, {}, function(content){
        $("div#card-back").html(content);
    });

    cardHeightBeforeFlip = $("div#card-container").height();
    $("div#card-container").height(cardHeightBeforeFlip);
    $("ul.vcard").fadeOut("fast",function(){
        $("div#card-container").animate({
            height: 360
        }, 300, function(){
            $("div#card-container").flip({
                direction:'tb',
                color: '#333',
                onBefore: function(){

                },
                onAnimation: function(){
                    //$("div#card-container").height(200);
                },
                onEnd: function(){
                    $("div#card-back").fadeIn();
                    if(func != false) func();
                }
            });
        });
    });
}

function changeEmailHash(el)
{
    window.location.hash = "email-"+el.attr("rel");
}

function emailCardFlip(el)
{
    var href = el.attr("href");
    cardFlip(el, href);
}

function embedCardFlip(el)
{
    var href = el.attr("href");
    cardFlip(el, href, function(){
		if(window.cardFlipped)
			window.cardFlipped();
    });
}

function changeLocationHash(el)
{
    window.location.hash = "location-"+el.attr("rel");
}

function embedHash(el)
{
    window.location.hash = "embed";
}

function locationCardFlip(el)
{
    var href = el.attr("href");

    cardFlip(el, href, function(){
        var geocoder = new google.maps.Geocoder();
        var latlng = new google.maps.LatLng(-34.397, 150.644);
        var myOptions = {
          zoom: 8,
          center: latlng,
          mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        var map = new google.maps.Map(document.getElementById("map"), myOptions);
        if (geocoder) {
          geocoder.geocode( { 'address': el.text()}, function(results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
              map.setCenter(results[0].geometry.location);
              var marker = new google.maps.Marker({
                  map: map,
                  position: results[0].geometry.location
              });
              $("#locating-map").hide();
              
            } else {
              //alert("Geocode was not successful for the following reason: " + status);
              $("#map").hide();
              $("#locating-map").hide();
              $("#no-map").show();
            }
          });
        }
    });
}

var prev_hash = "";
function checkHash()
{
    var hash = window.location.hash;
    if(prev_hash != hash) {
        if(hash != "" && hash != "#card")
        {
            
            if(hash.search("email") != -1) {
                //alert("email flip");
                var el = hash.replace("#email-", "");
                //alert(el);
                emailCardFlip($("dt a[rel="+el+"]"));
            }
            else if(hash.search("location") != -1) {
                var el = hash.replace("#location-", "");
                locationCardFlip($("dt a[rel="+el+"]"));
            }
            else if(hash.search("embed") != -1) {
                embedCardFlip($("a#embed_contact"));
            }
            
        } else {
            if(prev_hash != "") unflip();
        }
        prev_hash = hash;
    }
}

function periodicallyCheckHash()
{
    checkHash();
    setTimeout(function(){
        periodicallyCheckHash();
    }, 200);
}

var cardHeightBeforeFlip;

$(document).ready(function(){
    
    $("dt.email a").livequery(function(){
        var el = $(this);
        $(this).click(function(e){
            e.preventDefault();
            changeEmailHash(el);
        });
    });

    $("dt.location a").livequery(function(){
        var el = $(this);
        $(this).click(function(e){
            e.preventDefault();
            changeLocationHash(el);
        });
    });

    $("a#embed_contact").livequery(function(){
        var el = $(this);
        $(this).click(function(e){
            e.preventDefault();
            if(window.location.hash != "#embed") embedHash(el);
            else {
                unflip();
                window.location.hash = "card";
            }
        });
    });

    $("input.cancel").livequery(function(){
        $(this).click(function(e){
            e.preventDefault();
            unflip();
            window.location.hash = "card";
        });
    });

    periodicallyCheckHash();
});
