/* 
 * ajaxify.js
 * 
 * Copyright (c) 2011 Pink Donut <joaolopes at pinkdonut.net>. 
 * 
 * This file is part of CoreOnFire.
 * 
 * CoreOnFire is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * CoreOnFire is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with CoreOnFire.  If not, see <http ://www.gnu.org/licenses/>.
 */
(function(window, $,undefined){
var loadedImages, imageNumber, timeOut,theFuncToTime, needChange;
var xhr;
var History = window.History, // Note: We are using a capital H instead of a lower h
State = History.getState();

// Bind to State Change
History.Adapter.bind(window,'statechange',function(){ // Note: We are using statechange instead of popstate
    // Log the State
    var State = History.getState(); // Note: We are using History.getState() instead of event.state
    var url = State.url;
    
    getPage($, url);
    
});

$('.zoom a').live("click", function(){
    var _hr=$(this).attr("href");
    $(".vroom a[href='"+_hr+"']").click();
    return false;
});

//Numeros debaixo de cada projecto
$('.outros_projectos a').live("click", function(){
   var $this = $(this);
   if(!$this.hasClass("selected")){
        
        $('.outros_projectos a.selected').each(function(){
            var is_video = $(this).hasClass("video");
            $(this).removeClass("selected");
            if(is_video){
                $(this).find("img").attr("src", TEMPLATE_FOLDER + "img/video.png");
            }
        });
        $this.addClass("selected");
        var is_video = $this.hasClass("video");
        if(is_video){
            $(this).find("img").attr("src", TEMPLATE_FOLDER + "img/video_selected.png");
        }
        var description = html_entity_decode($this.data("description"));
        var projurl = html_entity_decode($this.data("projurl"));
        var projurlname = html_entity_decode($this.data("projurlname"));
        var title = html_entity_decode($this.data("title"));
        var background = html_entity_decode($this.data("background"));
        var media = html_entity_decode($this.data("media"));
        var mediafull = html_entity_decode($this.data("mediafull"));
        
        $('.projectdesc').html(description);
        $('.projurl').html(projurlname);
        $('.projurl').attr("href",projurl);
        $('.right_area h1:first').html(title);
        if(mediafull != '') {
            $('.zoom').html('<a href="'+mediafull+'">Zoom</a>');
            $('.vroom a').colorbox({opacity: 0.85, rel:'group_1'});
        }else{
            $('.zoom').html('');
        }
        
        //changeBackground('<img class="bg_img" src="'+ background + '" />');
        $('.preview_projecto .loading').stop(true,true).fadeTo(1000,'0');
        $('.preview_projecto .conteudo').hide(0,function(){
            $(this).remove();
            if(media.indexOf('youtube.com', 0) > -1){
                var youtubeVideoKey = media.substr(media.lastIndexOf("v=") + 2, 11);
                $('.preview_projecto').append('<div class="conteudo" style="display:none;" />');
                $('.preview_projecto .conteudo').append('<iframe width="559" height="362" src="http://www.youtube.com/embed/'+ youtubeVideoKey +'" frameborder="0" allowfullscreen></iframe>');
                $('.preview_projecto .conteudo').show(0);
                $('.preview_projecto .loading').stop(true,true).fadeOut();
            }else{
                
                var regExp = /http:\/\/(www\.)?vimeo.com\/(\d+)($|\/)/;
                var match = media.match(regExp);
                if (match){
                    $('.preview_projecto').append('<div class="conteudo" style="display:none;" />');
                    $('.preview_projecto .conteudo').append('<iframe src="http://player.vimeo.com/video/'+ match[2] +'?title=0&amp;byline=0&amp;portrait=0&amp;color=ffffff&amp;autoplay=1" width="559" height="362" frameborder="0" webkitAllowFullScreen allowFullScreen></iframe>');
                    $('.preview_projecto .conteudo').show(0);
                    $('.preview_projecto .loading').stop(true,true).fadeOut();
                }else{
                    media = '<img src="'+ media+'" style="display:none;" />';
                    $(media).one('load', function() {
                        var $this = $(this);
                        $this.css("display", 'none');
                        $('.preview_projecto').append('<div class="conteudo" />');
                        $this.appendTo($('.preview_projecto .conteudo'));
                        $('.preview_projecto .conteudo').find('img').show(0, function(){
                            $('.preview_projecto .loading').stop(true,true).fadeOut();
                        });
                    }).each(function() {
                        if(this.complete) $(this).load();
                    });
                }
                
            }
            
        });
        
   }
   return false;
});
var carousel;
var carouselIndex = 1;
var halt_hashChange = false;
$('.submenu a').live("click", function(){
    carouselIndex = 1;
    halt_hashChange = true;
    var url = $(this).attr("href");
    if ( History.enabled ) {
        History.pushState({
            url: url
        }, "", url);
    }else{
        $.bbq.pushState({
            url: url
        });
    }
    var _link = $(this).data("link");
    var back = $(this).data("background");
    $(".active").css("display", "none").removeClass("active");
    $(".projects_area[data-href='" + _link + "']").addClass("active").css("display", "block");
    carousel = $('.active .ohcarousel').data('jcarousel');
    //alert("set carousel to " + $('.active .ohcarousel').data('jcarousel'));
    $("a.left_arrow").unbind('click');
    $("a.left_arrow").bind("click",function(){
        carousel.prev();
        return false;
    });
    $("a.right_arrow").unbind('click');
    $("a.right_arrow").bind("click",function(){
        carousel.next();
        return false;
    });
    
    var imageHiddenHolder = $(".hidden_images");
    if(imageHiddenHolder.length == 0){
        $("#main").after('<div class="hidden_images" style="position: fixed; top: 0; left: 0; visibility: hidden; z-index: 1;"></div>');
        imageHiddenHolder = $(".hidden_images");
    }
    imageHiddenHolder.html('<img src="'+ back +'" />');
    loadImages();
    return false;
});
$('.ohcarousel a').live("click", function(){
    var url = $(this).attr("href");
    if ( History.enabled ) {
        History.pushState({
            url: url
        }, "", url);
    }else{
        $.bbq.pushState({
            url: url
        });
    }
    carouselIndex = $(this).parent().index() + 1;
    return false;
});
$('.fechar_projecto a').live("click", function(){
    var url = $(this).attr("href");
    if ( History.enabled ) {
        History.pushState({
            url: url
        }, "", url);
    }else{
        $.bbq.pushState({
            url: url
        });
    }
    return false;
});
$('.arrow_wrap.absolute a, a.logo_home_link, .menu a').live("click",function(){
    var url = $(this).attr("href");
    if ( History.enabled ) {
        History.pushState({
            url: url
        }, "", url);
    }else{
        $.bbq.pushState({
            url: url
        });
    }
    return false;
});



$(window).bind( "hashchange", function(e) {
    var url = $.bbq.getState( "url" );
    if(url !== undefined){
        getPage($, url);
    }
});
var lastParent = 0;
function getPage($, url){
    if(!halt_hashChange){
        if(xhr !== undefined){
            xhr.abort();
        }
        xhr = $.ajax({
            url: url,
            dataType:"json",
            data: "json=1",
            success: function(data){
                if(data.relocate !== undefined){
                    url = data.relocate;
                    if ( History.enabled ) {
                        History.pushState({
                            url: url
                        }, "", url);
                    }else{
                        $.bbq.pushState({
                            url: url
                        });
                    }
                }else{
                    PAGE_ID = data.id;
                    window.clearTimeout(timeOut);
                    theFuncToTime = function(){};
                    needChange = false;
                    if((data.parent_id >= 7 && data.parent_id <= 10) && (lastParent >= 7 && lastParent <= 10)){
                        changeDataNoAnim(data.body);
                    }else{
                        changeBackground(data.body);
                        changeData(data.body);
                    }
                    lastParent = data.parent_id;
                    $(".menu_wrapper .menu .selected").removeClass("selected");
                    if(data.activateMenu !== undefined){
                        $($(".menu_wrapper .menu li")[data.activateMenu]).addClass("selected");
                    }
                }
            }
        });
    }else{
        halt_hashChange = false;
    }
}

function neededDivsForDefault(body){
    var arrows;
    if($(".contents_load_wrapper").length == 0){
        $("#main").after('<div class="contents_load_wrapper" />');
    }
    if($(".content_container").length == 0){
        $(".contents_load_wrapper").append('<div class="content_container" />');
    }
    if($(".projecto_wrapper",body).length > 0) //Layout de single projecto
    {
        arrows = $(".arrow_wrap");
        if(arrows.length > 0){
            if(!arrows.hasClass("absolute")){
                arrows.fadeOut(500, function(){
                    arrows.remove();
                    $(".content_container").append('<div class="arrow_wrap absolute left"><a class="left_arrow ir">Previous</a></div><div class="arrow_wrap absolute right"><a class="right_arrow ir">Next</a></div>');
                    $(".arrow_wrap.right a").attr("href", $(".arrow_wrap.right a", body).attr("href"));
                    $(".arrow_wrap.left a").attr("href", $(".arrow_wrap.left a", body).attr("href"));
                });
            }else{
                $(".arrow_wrap.right a").attr("href", $(".arrow_wrap.right a", body).attr("href"));
                $(".arrow_wrap.left a").attr("href", $(".arrow_wrap.left a", body).attr("href"));
            }
        }else{
            $(".content_container").append('<div class="arrow_wrap absolute left"><a class="left_arrow ir">Previous</a></div><div class="arrow_wrap absolute right"><a class="right_arrow ir">Next</a></div>');  
            $(".arrow_wrap.right a").attr("href", $(".arrow_wrap.right a", body).attr("href"));
            $(".arrow_wrap.left a").attr("href", $(".arrow_wrap.left a", body).attr("href"));
        }
        
        
    }else{ // END LAYOUT de SINGLE PROJECTO
       if($(".projects_wrapper", body).length >0){ //Encontrado lista de projectos
           arrows = $(".arrow_wrap");
           if(arrows.hasClass("absolute")){
               arrows.fadeOut(500, function(){
                   arrows.remove();
               });
           }
       }else{
           if($("#contactos",body).length>0){ // Encontrada a área de contactos
               //clearBackground();
               
           } 
           arrows = $(".arrow_wrap");
           arrows.fadeOut(500, function(){
               arrows.remove();
           });
       }
    }
    $(window).trigger("resize");
}
function clearBackground(){
   $("#background img, #background h1").fadeOut(1000, function(){$(this).remove()});
}
function changeData(body){
    if(PAGE_ID === 1){
        if($(".contents_load_wrapper").length > 0) {
            fadeOutAndRemove($(".contents_load_wrapper"));
        }
    }else{
        neededDivsForDefault(body);
        var mycontent = $(".mycontent", body);
        if(mycontent.length > 0){
            if($(".mycontent").length > 0) codaOutAndRemove($(".mycontent"), function(){
                codaBubbleAdd(mycontent,$(".content_container"));
            });
            else
                codaBubbleAdd(mycontent,$(".content_container"));
            //slideInAndAdd(mycontent, $(".content_container"));
            
        }
    }
}

function changeDataNoAnim(body){
    if(PAGE_ID === 1){
        if($(".contents_load_wrapper").length > 0) {
            fadeOutAndRemove($(".contents_load_wrapper"));
        }
    }else{
        neededDivsForDefault(body);
        var mycontent = $(".mycontent", body);
        if(mycontent.length > 0){
            if($(".mycontent").length > 0) simpleChange($(".mycontent"), mycontent,$(".content_container"));
            else
                codaBubbleAdd(mycontent,$(".content_container"));
            //slideInAndAdd(mycontent, $(".content_container"));
            
        }
    }
}

function changeDataFade(body){
    if(PAGE_ID === 1){
        if($(".contents_load_wrapper").length > 0) {
            fadeOutAndRemove($(".contents_load_wrapper"));
        }
    }else{
        neededDivsForDefault(body);
        var mycontent = $(".mycontent", body);
        if(mycontent.length > 0){
            if($(".mycontent").length > 0) codaOutAndRemoveFade($(".mycontent"), function(){
                codaBubbleAddFade(mycontent,$(".content_container"));
            });
            else
                codaBubbleAddFade(mycontent,$(".content_container"));
            //slideInAndAdd(mycontent, $(".content_container"));
            
        }
    }
}

function simpleChange($stuff, $that,prependTo){
    $that.hide();
    if($that.find("#contactos").length >0){
        $that.css("width", "100%");
        $that.css("height", "100%");
    }
    $that.prependTo(prependTo);
    if($that.find(".preview_projecto").length > 0){//FAZER LOAD DA IMAGEM PRIMEIRO
        var $images = $(".preview_projecto img");
            var loadedImages = new Array();
            var c = 0;
            if($images.length > 0){
                $images.one('load', function() {
                    var $this = $(this);
                    var mc = c++;
                    loadedImages[mc] = $this;
                    if(c == $images.length){
                        $stuff.remove();
                        $that.show(0);
                    }
                }).each(function() {
                    if(this.complete) {
                        $(this).load();
                    }
                });
            }else{
                $stuff.remove();
                $that.show(0);
            }
    }else{
        $stuff.remove();
        $that.show(0);
    }
    if($that.find(".ohcarousel").length > 0){
        startCarousel();
    }
    if($that.find(".vroom").length > 0){
        $('.vroom a').colorbox({opacity: 0.85, rel:'group_1'});
    }
    startMap();
}

function goAndSlide($that){
    var left = $that.width();
    $that.css({
        width: left + "px" 
    })
    $that.css({
       position: "absolute",
       top: "50%",
       left: "100%",
       marginLeft: left,
       marginTop: ($that.height()/2)*-1
    });
    
    $that.css("visibility", "visible");
    $that.animate({
        left: "50%",
        marginLeft: (left/2) * -1
    },1000,function(){
        $that.attr("style", '');
    });
    if($that.find(".ohcarousel").length > 0){
        startCarousel();
    }
    if($that.find(".vroom").length > 0){
        $('.vroom a').colorbox({opacity: 0.85, rel:'group_1'});
    }
}
function codaBubbleAdd($that,prependTo){
$that.css({
        "margin-top": "50px",
        opacity: 0
    });
    if($that.find("#contactos").length >0){
        $that.css("width", "100%");
        $that.css("height", "100%");
    }
    $that.prependTo(prependTo);
    if($that.find(".preview_projecto").length > 0){//FAZER LOAD DA IMAGEM PRIMEIRO
        var $images = $(".preview_projecto img");
            var loadedImages = new Array();
            var c = 0;
            if($images.length > 0){
                $images.one('load', function() {
                    var $this = $(this);
                    var mc = c++;
                    loadedImages[mc] = $this;
                    if(c == $images.length){
                        $that.animate({opacity: 1, "margin-top": 0});
                    }
                }).each(function() {
                    if(this.complete) $(this).load();
                });
            }else{
                $that.animate({opacity: 1, "margin-top": 0});
            }
    }else{

        $that.animate({opacity: 1, "margin-top": 0});
    }
    if($that.find(".ohcarousel").length > 0){
        startCarousel();
    }
    if($that.find(".vroom").length > 0){
        $('.vroom a').colorbox({opacity: 0.85, rel:'group_1'});
    }
    startMap();
}

function codaBubbleAddFade($that,prependTo){
    $that.hide();
    if($that.find("#contactos").length >0){
        $that.css("width", "100%");
        $that.css("height", "100%");
    }
    $that.prependTo(prependTo);
    if($that.find(".preview_projecto").length > 0){//FAZER LOAD DA IMAGEM PRIMEIRO
        var $images = $(".preview_projecto img");
            var loadedImages = new Array();
            var c = 0;
            if($images.length > 0){
                $images.one('load', function() {
                    var $this = $(this);
                    var mc = c++;
                    loadedImages[mc] = $this;
                    if(c == $images.length){
                        $that.fadeIn(250);
                    }
                }).each(function() {
                    if(this.complete) $(this).load();
                });
            }else{
                $that.fadeIn(250);
            }
    }else{

        $that.fadeIn(250);
    }
    if($that.find(".ohcarousel").length > 0){
        startCarousel();
    }
    if($that.find(".vroom").length > 0){
        $('.vroom a').colorbox({opacity: 0.85, rel:'group_1'});
    }
    startMap();
}
function slideInAndAdd($that, prependTo){
    
    $that.css("visibility", "hidden");
    $that.css({
       position: "absolute",
       top: "0",
       left: "0"
    });
    $that.css({
        overflow: "hidden"
    });
    if($that.find("#contactos").length >0){
        $that.css("width", "100%");
        $that.css("height", "100%");
    }
    $that.prependTo(prependTo);
    if($that.find(".preview_projecto").length > 0){//FAZER LOAD DA IMAGEM PRIMEIRO
        var $images = $(".preview_projecto img");
        var loadedImages = new Array();
        var c = 0;
        if($images.length > 0){
            $images.one('load', function() {
                var $this = $(this);
                var mc = c++;
                loadedImages[mc] = $this;
                if(c == $images.length){
                    goAndSlide($that);
                }
            }).each(function() {
                if(this.complete) $(this).load();
            });
        }else{
            goAndSlide($that);
        }
        
    }else{
        goAndSlide($that);
    }
    
}

function codaOutAndRemove($that,callback){

    $that.animate({
        opacity: 0,
        marginTop: "-50px"
    },500,function(){
        $that.remove();
        callback();
    });
    
    //$that.fadeOut(500,function(){
      //  $that.remove();
    //});
}

function codaOutAndRemoveFade($that,callback){
    $that.fadeOut(250, function(){
        $that.remove();
        callback();
    });
    
    //$that.fadeOut(500,function(){
      //  $that.remove();
    //});
}
function slideOutAndRemove($that){
    $that.css("position", "absolute");
    $that.css({
       position: "absolute",
       top: "50%",
       left: "50%",
       marginTop: ($that.height() /2) * -1,
       marginLeft: ($that.width() /2) * -1
    });
    $that.animate({
        left: "-50%",
        marginLeft: ($that.width() /2) * -1
    },1000,function(){
        $that.remove();
    });
}
function fadeOutAndRemove($that){
    $that.fadeOut(1000, function(){
        $that.remove();
    })
}
function changeBackground(body){
    var $images;
    if(body === undefined){
        $images = $(".bg_img");
    }else{
        $images = $(body).find(".bg_img");
        if($images.length === 0){
            $images = $(body);
        }
    }
    if($images.length > 0){
        var imageHiddenHolder = $(".hidden_images");
        if(imageHiddenHolder.length == 0){
            $("#main").after('<div class="hidden_images" style="position: fixed; top: 0; left: 0; visibility: hidden; z-index: 1;"></div>');
            imageHiddenHolder = $(".hidden_images");
        }
        
        $images.each(function(){
            $(this).appendTo(imageHiddenHolder);
        });
        loadImages();
    }
}
if(!History.enabled)
    $(window).trigger("hashchange");

function loadImages(){
    var $images = $(".hidden_images img");
    loadedImages = new Array();
    var c = 0;
    imageNumber = 0;
    var tLoaded = 0;
    var tempArray = new Array();
    $images.one('load', function() {
        var $this = $(this);
        var mc = c++;
        if(PAGE_ID===1){
            if($this.attr('alt') == 'PUBLICIDADE') loadedImages[1] = $this;
            else if($this.attr('alt') == 'DESIGN') loadedImages[2] = $this;
            else if($this.attr('alt') == 'NEW MEDIA') loadedImages[3] = $this;
            else if($this.attr('alt') == 'EVENT MARKETING') loadedImages[4] = $this;
            else if($this.attr('alt') == 'COMUNICAÇÃO 360º') loadedImages[0] = $this;
        }else{
            loadedImages[mc] = $this;
        }
        if(c == $(".hidden_images img").length){
            $(".hidden_images").remove();
            changeImageFromBackground();
        }
    }).each(function() {
        if(this.complete) $(this).load();
        
    });
}
function timerChangeBackground(){
    if(needChange){
        if(imageNumber >= loadedImages.length){
            imageNumber = 0;
        }
        var image = loadedImages[imageNumber++];
        $("#background").append(image);
        image.attr("width", "100%");
        resizeWindow(); //Defined in resizeChecker.js
        image.css("display",'none');
        if(PAGE_ID === 1){
            //$("#background .overlay").fadeOut();
            if($("#background h1").length > 0){
                $("#background h1").fadeOut(200, function(){
                    $("#background h1").html(image.attr("alt"));
                    if(image.attr("alt") == 'COMUNICAÇÃO 360º'){
                        $("#background h1").css("font-size", "40px");
                        $("#background h1").css("line-height", "40px");
                    }else{
                        $("#background h1").css("font-size", "60px");
                        $("#background h1").css("line-height", "60px");
                    }
                    $("#background h1").fadeIn(1000);
                });
            }else{
                $("#background").append("<h1>"+image.attr("alt")+"</h1>");
                if(image.attr("alt") == 'COMUNICAÇÃO 360º'){
                    $("#background h1").css("font-size", "40px");
                    $("#background h1").css("line-height", "40px");
                }else{
                    $("#background h1").css("font-size", "60px");
                    $("#background h1").css("line-height", "60px");
                }
            }
        }else{
            $("#background .overlay").fadeIn();
            if($("#background h1").length > 0){
                $("#background h1").fadeOut(1000, function(){
                    $("#background h1").remove();
                });
            }
        }
        image.fadeIn(1000, function(){
            $("#background img:not(:last)").remove();
            window.clearTimeout(timeOut);
            theFuncToTime = timerChangeBackground;
            timeOut = window.setTimeout(theFuncToTime, 5000);
            resizeWindow();
        });
    }
}
function changeImageFromBackground(){
    if(imageNumber >= loadedImages.length){
        imageNumber = 0;
    }
    var image = loadedImages[imageNumber++];
    $("#background").append(image);
    image.attr("width", "100%");
    resizeWindow(); //Defined in resizeChecker.js
    image.css("display",'none');
    if(PAGE_ID === 1){
        //$("#background .overlay").fadeOut();
        if($("#background h1").length > 0){
            $("#background h1").fadeOut(200, function(){
                $("#background h1").html(image.attr("alt"));
                if(image.attr("alt") == 'COMUNICAÇÃO 360º'){
                    $("#background h1").css("font-size", "40px");
                    $("#background h1").css("line-height", "40px");
                }else{
                    $("#background h1").css("font-size", "60px");
                    $("#background h1").css("line-height", "60px");
                }
                $("#background h1").fadeIn(1000);
            });
        }else{
            $("#background").append("<h1>"+image.attr("alt")+"</h1>");
            if(image.attr("alt") == 'COMUNICAÇÃO 360º'){
                $("#background h1").css("font-size", "40px");
                $("#background h1").css("line-height", "40px");
            }else{
                $("#background h1").css("font-size", "60px");
                $("#background h1").css("line-height", "60px");
            }
        }
    }else{
        $("#background .overlay").fadeIn();
        if($("#background h1").length > 0){
            $("#background h1").fadeOut(1000, function(){
                $("#background h1").remove();
            });
        }
    }
    image.fadeIn(1000, function(){
        $("#background img:not(:last)").remove();
        if(loadedImages.length > 1){
            needChange = true;
            theFuncToTime = timerChangeBackground;
            window.clearTimeout(timeOut);
            timeOut = window.setTimeout(timerChangeBackground, 5000);
        }
    });
}
changeBackground(undefined);

function startCarousel(){
    if($(".ohcarousel").length > 0){
            var isiPad = navigator.userAgent.match(/iPad/i) != null;
            $(".ohcarousel").each(function(){
                /*$(this).skaCarousel({
                    width: 113,
                    height: 115
                });*/
                $(this).jcarousel({
                    wrap: 'circular',
                    start: carouselIndex,
                    initCallback: function(carouel){
                        carousel = $('.active .ohcarousel').data('jcarousel');
                        $("a.left_arrow").unbind('click');
                        $("a.left_arrow").bind("click",function(){
                            carousel.prev();
                            return false;
                        });
                        $("a.right_arrow").unbind('click');
                        $("a.right_arrow").bind("click",function(){
                            carousel.next();
                            return false;
                        });
                    },
                    buttonNextHTML: null,
                    buttonPrevHTML: null,
                    itemFallbackDimension: 113
                });
                carouselIndex = 1;
            });
    }
}

function startMap(){
    if($(".map_area").length > 0){
        $('.map_area').gMap();
    }
   if($("#contactos").length > 0){
        $('#map').gMap(
        {
             //address: "Rua Fernão Vaz Dourado 62 4150-322 Porto Portugal",
             maptype: 'SATELLITE',
             scrollwheel: true,
             zoom: 15,
             latitude: 41.16081,
             longitude: -8.66304,
             markers: [
                {
                    address: "Rua Fernão Vaz Dourado 62 4150-322 Porto Portugal",
                    html: "<span style='color:red'>Porto</span><br />Rua Fernão Vaz Dourado 62<br />4150-322 Porto Portugal<br />T +351 226 165 560<br />F +351 226 165 570<br />E <a href=\"mailto:ska@ska.pt\" style=\"color:#000\">ska@ska.pt</a>"
                },
                {
                    address: "Av João Crisóstomo 30 - 6.º Esq. 1050-127 Lisboa Portugal",
                    html: "<span style='color:red'>Lisboa</span><br />Av João Crisóstomo 30 - 6.º Esq.<br />1050-127 Lisboa Portugal<br />T +351 213 195 640<br />F +351 213 156 261<br />E <a href=\"mailto:ska@ska.pt\" style=\"color:#000\">ska@ska.pt</a>"
                }
             ],
             controls: {
                 panControl: true,
                 zoomControl: true,
                 mapTypeControl: true,
                 scaleControl: false,
                 streetViewControl: true,
                 overviewMapControl: false
             },
             icon: {
                image: TEMPLATE_FOLDER+"img/icons/marcador.png",
                shadow: TEMPLATE_FOLDER+"img/icons/sombra_marcador.png",
                iconsize: [70, 65],
                shadowsize: [115, 30],
                iconanchor: [36, 62],
                shadowanchor: [60, 15]
            }
        });
        var position = $('#main .menu_wrapper').offset();
        $('#infArea').css('position', 'absolute');
        $('#infArea').css('left', position.left);
        $('#infArea').css('top', '150px');
        $('#infArea').fadeIn();

        $('#linkToLisboa').bind('click', function(){
            $('#map').gMap('centerAt',
                {
                        latitude: 38.734608,
                        longitude: -9.149766,
                        zoom: 15
                }
            );
        });

        $('#linkToPorto').bind('click', function(){
            $('#map').gMap('centerAt',
                {
                        latitude: 41.16081,
                        longitude: -8.66304,
                        zoom: 15
                }
            );
        });
        
    } 
}
$(".agencia.arrow_wrap div.left_arrow").live("click",function(){
    $(".agencia.arrow_wrap div.left_arrow").parent().hide();
    $(".agencia.arrow_wrap div.right_arrow").parent().show();
    if($("#agencia .ag1").css("display") == "none"){
            $("#agencia .ag2").fadeOut(500, function(){$("#agencia .ag1").fadeIn()});
    }else{
            $("#agencia .ag1").fadeOut(500, function(){$("#agencia .ag2").fadeIn()});
    }
    return false;
});
$(".agencia.arrow_wrap div.right_arrow").live('click', function(){

    $(".agencia.arrow_wrap div.left_arrow").parent().show();
    $(".agencia.arrow_wrap div.right_arrow").parent().hide();
	if($("#agencia .ag1").css("display") == "none"){
		$("#agencia .ag2").fadeOut(500, function(){$("#agencia .ag1").fadeIn()});
	}else{
		$("#agencia .ag1").fadeOut(500, function(){$("#agencia .ag2").fadeIn()});
	}
	return false
});

$(".preview_projecto").live("click",function(){
    var ob = $(".outros_projectos a.selected");
    var total = $(".outros_projectos a").length;
    if(ob.index() === total - 1){
        $(".outros_projectos a:first").click();
    }else{
        ob.next("a").click();
    }
});
$('.vroom a').colorbox({opacity: 0.85, rel:'group_1'});
startCarousel();
startMap();
})(window, jQuery);

