/**
 * @author Michał Matoga <michalmatoga@eura7.com>
 * @copyright eura7.com
 * @package applycapnor
 * @version 1.0.0
 */
$(document).ready(function(){
    
    Cufon.replace('ul#main-menu li.active h2 a', {
        color: '-linear-gradient(#79bde2, #5a9ec5)'
    });
    Cufon.replace('ul#main-menu li h2 a', {
        hover: {
            color: '#c4eaff'
        }
    });
    Cufon.replace('div#content h2, div#top-portfolio h2', {
        color: '-linear-gradient(#476689, #002b5c)'
    });
    //portfolio
    Cufon.replace('ul#categories-menu li.active a', {
        color: '#7e7e7e',
        hover: {
            color: '#7e7e7e'
        }
    });
    Cufon.replace('ul#categories-menu li a, ul#categories-menu li a', {
        color: '#fff',
        hover: {
            color: '#7e7e7e'
        }
    });
    Cufon.replace('ul#categories-menu li.active a', {
        color: '#7e7e7e'
    });
    //lista kategorii portfolio
    Cufon.replace('div#column-left.category-list h3', {
        color: '#fff'
    });

    //listing portfolio
    Cufon.replace('div#project-listing ul li div h4', {
        color: '#fff'
    });
    //szczegóły projektu nagłówki
    Cufon.replace('div#project-details h4', {
        color: '-linear-gradient(#476689, #002b5c)'
    });
    
    

    /**
     *
     *  Aktualnie zaznaczona kropka w topie
     */
    var currentDotTop = 1;

    /**
     *  Aktualnie zaznaczone kropka w realizacjach
     */
    var currentDotAccomplishment = 1;

    /**
     *  Odstęp czasu pomiędzy animacjami zmiany zawartości na stronie [ms]
     */
    var autoInterval = 10000;

    /**
     *  Czas trwania animacji zmiany zawartości
     */
    var switchSpeed = 500;

    /**
     *  Czy włączyć automatyczne zmienianie treści top/realizacje
     */
    var autoSwitching = true;

    /**
     *  Timer odstępu pomiędzy kolejnymi wydarzeniami zmiany zawartości topu
     */
    var intervalTop = null;

    /**
     *  Timer odstępu pomiędzy kolejnymi wydarzeniami zmiany zawartości realizacji
     */
    var intervalAccomplishments = null;

    /**
     * Sterowanie tłem w topie
     * @param dotNumber numer kropki, która ma zostać zaznaczona oraz numer obrazka, który będzie widoczny
     * @param speed czas trwania animacji zmiany obrazka [ms]
     */
    function switchTopTo(dotNumber, speed){
        var dot = $('ul.navi.top li.dot-'+dotNumber);
        dot.siblings().removeClass('active');
        dot.queue(function(){
            $(this).parent().parent().children('.img-'+dotNumber).fadeIn(speed);
            $(this).parent().parent().children('.active').fadeOut(speed);
            $(this).parent().parent().children('.active').removeClass('active');
            $(this).parent().parent().children('.img-'+dotNumber).addClass('active');
            $(this).dequeue();
        });
        dot.addClass('active');
        currentDotTop = dotNumber;
    }

    $('ul.navi.top li').click(function(event){
        event.stopPropagation();
        event.preventDefault();
        if (!$(this).hasClass('active')){
            var dotNumber = $(this).attr('class').substr(4,1);
            switchTopTo(dotNumber, switchSpeed);
            clearInterval(intervalTop);
            startIntervalTop();
        }
    });

    /**
     *  Automatyczna zmiana tła w topie
     */
    function startIntervalTop(){
        var dotCount = $('ul.navi.top li').length;
        if (autoSwitching)
            intervalTop = setInterval(switchTopToAuto, autoInterval);
        function switchTopToAuto(){
            if (currentDotTop++ == dotCount)
                currentDotTop = 1;
            switchTopTo(currentDotTop, switchSpeed);
        }
    }
    /**
     *  Wywołanie powyższej funkcji onDomReady
     */
    $(function() {
        startIntervalTop();
    });


    /******************************************************************************************************
     * Sterowanie realizacjami
     *
     * Content realizacji jest ładowany z obiektu JSON trzymanego w zmiennej accomplishments zainicjalizowanej
     * w widoku.
     *
     */


    /**
     * Sterowanie tłem w topie
     * @param dotNumber numer kropki, która ma zostać zaznaczona oraz numer obrazka, który będzie widoczny
     * @param speed czas trwania animacji zmiany obrazka [ms]
     */
    function switchAccomplishmentTo(dotNumber, speed){
        var dot =  $('ul.navi.bottom li.dot-'+parseInt(dotNumber));
        dot.siblings().removeClass('active');
        //console.log(parseInt(dotNumber));
        $('div#column-left-lower').fadeOut(speed);
        $('div#column-left-lower').queue(function(){
            $(this).children('a').first().attr('href', '/' + lang + '/' + projectsUrl + '/'
                + accomplishments[parseInt(dotNumber)]['cat_url'] + '/'
                + accomplishments[parseInt(dotNumber)]['name_url']);
            $(this).children('a').first().attr('title', accomplishments[parseInt(dotNumber)]['name'].replace('?coma?', ','));
            $(this).css('background', 'url("/gfx-portfolio/'+accomplishments[parseInt(dotNumber)]['img']+'")');
            $(this).dequeue();
        });
        $('div#column-left-lower').fadeIn(speed);
        dot.parent().siblings('div').fadeOut(speed);
        dot.parent().siblings('div').queue(function(){
            $(this).children('a.name').html(accomplishments[parseInt(dotNumber)]['short_name'].replace('?coma?', ','));
            $(this).children('a.name').attr('href', '/' + lang + '/' + projectsUrl + '/'
                + accomplishments[parseInt(dotNumber)]['cat_url'] + '/'
                + accomplishments[parseInt(dotNumber)]['name_url']);
            $(this).children('a.name').attr('title', accomplishments[parseInt(dotNumber)]['name'].replace('?coma?', ','));
            $(this).children('a.right').attr('href', '/' + lang + '/' + projectsUrl + '/'
                + accomplishments[parseInt(dotNumber)]['cat_url'] + '/'
                + accomplishments[parseInt(dotNumber)]['name_url']);
            $(this).children('p.desc').html(accomplishments[parseInt(dotNumber)]['desc'].replace('?coma?', ','));
            $(this).dequeue();
        });
        dot.parent().siblings('div').fadeIn(speed);
        dot.addClass('active');
        currentDotAccomplishment = parseInt(dotNumber);
    }

    $('ul.navi.bottom li').click(function(event){
        event.stopPropagation();
        event.preventDefault();
       
        if (!$(this).hasClass('active')){
            var dotNumber = $(this).attr('class').substr(4,2);
            switchAccomplishmentTo(parseInt(dotNumber));

            clearInterval(intervalAccomplishments);
            startIntervalAccomplishment();
        }
    });

    /**
     *  Automatyczna zmiana realizacji
     */
    function startIntervalAccomplishment(){
        var dotCount = $('ul.navi.bottom li').length;
        if (autoSwitching && dotCount > 1)
            intervalAccomplishments = setInterval(switchAccomplishmentToAuto, autoInterval);
        function switchAccomplishmentToAuto(){
            if (currentDotAccomplishment++ == dotCount)
                currentDotAccomplishment = 1;
            switchAccomplishmentTo(currentDotAccomplishment, switchSpeed);
        }
    }
    /**
     *  Wywołanie powyższej funkcji onDomReady
     */
    $(function() {
        startIntervalAccomplishment();
    });

    $('ul#categories-menu li').mouseenter(function(){
        $(this).addClass('hover');
    });
    $('ul#categories-menu li').mouseleave(function(){
        $(this).removeClass('hover');
    });

    /**
     * Rozwijanie listy kategorii portfolio
     

    $('div#column-left.category-list div.header').click(function(){
        if (!$(this).hasClass('active')){
            $(this).queue(function(){
                $('div#column-left.category-list div.header.active').removeClass('active');
                $('div#column-left.category-list ul.active li').css('display', 'none');
                $('div#column-left.category-list ul.active').slideUp('fast');
                $('div#column-left.category-list ul.active').removeClass('active');
                $(this).addClass('active');
                $(this).next().children('li').css('display', 'list-item');
                $(this).next().slideDown('fast');
                $(this).next().addClass('active');
                $(this).dequeue();
            });
        }
    })
    
    */
    /**
     * Animacja warstwy w listingu projektów
     */

    $('div#project-listing ul li').mouseenter(function(event){
        $(this).children('div').animate({
            bottom: '-12'
        }, 'fast');
    })
    $('div#project-listing ul li').mouseleave(function(event){
        $(this).children('div').animate({
            bottom: '-51'
        }, 'fast');
    })

    /**
     * Otwieranie zewnętrznych stron w nowych kartach
     */
    $('div#tru-list ul li a').attr('target', 'blank');
    $('div#panoram-list ul li a').attr('target', 'blank');
    $('div#other-list ul li a').attr('target', 'blank');
    $('a.brochure.button').attr('target', 'blank');
    $('ul#files a').attr('target', 'blank');


    if ($('#flash-window').length != 0){
        $("#flash-window").flash({
            src: "public/flash/player.swf",
            width: 650,
            height: 480,
            wmode: "transparent",
            allowfullscreen: "true",
            flashvars: {
                xml_url: $("#flash-window").children().first().html()
            }
        },
        {
            expressInstall: true
        }
        );
    }
    if($('#map').length != 0){
        initializeMap();
    }

    if ($('#flashbanner').length != 0){
        $("#flashbanner").flash({
            src: $("#flashbanner").children().first().html(),
            width: 988,
            height: 285,
            wmode: "transparent"
        },
        {
            expressInstall: true
        }
        );
    }
    $('a.highslide').click(function(event){
        event.preventDefault();
    });
      
});



function initializeMap() {
    var myLatlng = new google.maps.LatLng(50.08252,19.994205)
    var myOptions = {
        zoom: 15,
        center: myLatlng,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    }
    var map = new google.maps.Map(document.getElementById("map"), myOptions);

    var contentString = '<div id="map-cloud"><div></div></div>';

    var infowindow = new google.maps.InfoWindow({
        content: contentString,
        maxWidth: 100
    });

    var marker = new google.maps.Marker({
        position: new google.maps.LatLng(50.07952,19.994205),
        map: map,
        title:"Apply Capnor"
    });
    infowindow.open(map,marker);
}

function showMail(e,d,f){
    document.write('<a href="mailto:'+e+"@"+d+"."+f+'">'+e+"@"+d+"."+f+"</a>")}
