var _localScroll = 0;
var _interval = null;
var _pitch = 15;
var anim = true;
var animBlock = true;

Dana = new function()
{
    var _currentInterval;
    var _defaultSlideshow = $($('div.sectionContent')[0]).find('ul.slideshow');
    var _mapState = 'normal';
    var _map;
    var _marker;
    var _currentPosition;
    var _streetView;
    
    this.init = function()
    {
        Dana.initCufon();
        Dana.initMap();
        
        
        if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)) || (navigator.userAgent.match(/iPod/i))) {
            $('.slideshow li').get(0).removeClass('hidden') ;
            
            Dana.launchSlideShow(_defaultSlideshow);
            Dana.parseNewsFeed();
            Dana.startNewsFeed();
        }else{
            $('shortContainer').css('position', 'absolute') ;
            var feeds = $('.newsFeed') ;
            var feedstitle = $('.newsFeedTitle').text() ;
            var feedstext = $('p.dynamic').html();
            feedstext = feedstext.split('\n').join('  -  ');
            
            feeds.remove() ;
            
            var images = $('li img', $('.slideshow').eq(0)).map(function(i, el){
                return el.src ;
            }).toArray() ;

			images[images.length] = images[0];
            images.shift() ;
            images.join(',').replace(/"/g, '') ;
            
            var flashvars = {
                slideshow_duration:3000 ,
                slideshow_speed:2000 ,
                images:images,
                requestedFont:"Helvetica" ,
                fallbackFont:"Arial" ,
                fontColor:"#FFFFFF" ,
                fontSize:"13" ,
                defaultFeedTitle:'NEWS',
                feeds:feedstext
            } ;
            
            var params = {menu: "false", quality:'high', wmode:"transparent"} ;
            var attributes = {id:"flashHome", name:"flashHome"} ;
            
            swfobject.embedSWF("./home.swf", "DanaHomeFlash", "910", "570", "10.0.0", "expressInstall.swf", flashvars, params, attributes);
            $('.slideShow').hide();
        }
    }
    
    this.initMap = function()
    {
        //var latlng = new google.maps.LatLng(48.867717,2.275177);
        var latlng = new google.maps.LatLng(48.86481389999999,2.27365050);
        var mapOptions = {
          zoom: 15,
          center: latlng,
          mapTypeId: google.maps.MapTypeId.ROADMAP,
          streetViewControl : false
        };
        _map = new google.maps.Map(document.getElementById("mapBox"), mapOptions);
        
        _marker = new google.maps.Marker({
            position : _map.getCenter(),
            map : _map
        });
    }
    
    this.scaleMap = function()
    {
        switch(_mapState) {
            case 'normal':
                $('.contactContent').animate({
                    'width':'810px'
                }, 500, 'swing', function()
                {
                    _mapState = 'big';
                    Dana.openStreetView();
                });
                break;
            case 'big':
                $('.contactContent').animate({
                    'width':'350px'
                }, 500, 'swing', function()
                {
                    _mapState = 'normal';
                    Dana.closeStreetView();
                });
                break;
        }
    }
    
    this.openStreetView = function()
    {
        _currentPosition = _marker.position;
        
        _streetView = new google.maps.StreetViewPanorama(document.getElementById("mapBox"), { position : _currentPosition });
        _map.setStreetView(_streetView);
    }
    
    this.closeStreetView = function()
    {
        Dana.initMap();
    }
    
    this.initCufon = function()
    {
        var all_hoverables = { 
		    h2: true,
		    h3: true,
		    p: true,
		    span: true,
		    a: true
	    };
        
        Cufon.replace('ul.sectionMenu li a', {hover: true, hoverables: all_hoverables});
        Cufon.replace('ul.listBlocks li a', {hover: true, hoverables: all_hoverables});
        Cufon.replace('h1', {hover: true, hoverables: all_hoverables});
        Cufon.replace('h2', {hover: true, hoverables: all_hoverables});
        Cufon.replace('h3.newsTitle');
        Cufon.replace('ul.contactMenu li');
        Cufon.replace('ul.langList li');
        Cufon.replace('ul.langList li a');
        Cufon.replace('ul.homeShortcuts li a', {hover: true, hoverables: all_hoverables});
    }
    
    this.launchSlideShow = function(slideshow)
    {
        var index = 0;
        var slideshowLength = $(slideshow).find('li').length;
        
        _currentInterval = setInterval(function()
        {
            var current = $(slideshow).find('li')[index];
            
            $(current).hide('fade', 1500);
            
            if(index == slideshowLength - 1) index = 0;
            else index++;
            
            var next = $(slideshow).find('li')[index];
            
            $(next).show('fade', 1500);
            
        }, 5000);
    }
    
    this.parseNewsFeed = function()
    {
        var feeds = $('p.dynamic').html();
        feeds = feeds.split('\n').join('<span class="newsSpace">&bull;</span>');
        $('p.dynamic').html(feeds);
    }
    
    this.startNewsFeed = function()
    {
        $('p.dynamic').marquee().mouseover(function(evt)
        {
            $(this).trigger('stop');
        }).mouseout(function(evt)
        {
            $(this).trigger('start');
        });
    }
    
    this.stopSlideShow = function()
    {
        clearInterval(_currentInterval);
    }
}

Dana.init();

$(document).ready(function (){
    
    $('a.ajaxLink').click(function(e)
    {
        e.preventDefault();
        
        var url = $(this).attr('href');
        
        $.fancybox({
            type : 'ajax',
            href : url,
            overlayColor : '#000',
            onComplete : function()
            {
                Cufon.replace('#legalsContent h1');
            }
        });
    });
    
    $("ul.sectionMenu li a, div.logo a").click(function(evt)
    {
        // Stop every slideshow
        Dana.stopSlideShow();
        
    	if(!$(this).hasClass('extLink')) evt.preventDefault();
    	else return true;
        
        // Check if anim
        if(anim != true) return false;
        // False = state to block anim
        anim = false;
        
        var id = $(this).attr('href');
        id = id.substring(id.lastIndexOf('-')+1, id.length);
        
        var height = 0;
        
        $("ul.sectionMenu li a").removeClass('selected');
        $(this).addClass('selected');

        $(".scrollContent > div").each(function(index, section){
            
            if(index == id) {
                
                var listBlocks = $(section).find(".listBlocks");
                
                if (listBlocks && !($(listBlocks).find(".blockSelected").length) ) {
                    
                    listBlocks.find("li:first-child a").trigger("click");
                }
                
                var slide = $(this).find('ul.slideshow');
                if(slide.length > 0) {
                    Dana.launchSlideShow($(slide));
                } else {
                    Dana.stopSlideShow();
                }
                return false;
            }
            height += $(this).height();
        });
        

        $("#content").animate({scrollTop: height}, 1000, "swing", function()
        {
            // Allowing animation if previous anim ended
            anim = true;
        });
    });
    
    $('ul.homeShortcuts li a').click(function(evt)
    {
        evt.preventDefault();
        
        var current = this;
        var id = $(this).attr('href');
        var section = '#'+$(this).attr('rel');
        
        $(section + ' ul.listBlocks li a').each(function(index, link)
        {
            if($(link).attr('href') == $(current).attr('href')) $(link).trigger('click');
        });
        
        var sectionId = $(section).parent().attr('id');
        
        $($("ul.sectionMenu li a[href="+ section +"]")[sectionId - 1]).trigger('click');
        sectionId = sectionId.substring(sectionId.lastIndexOf('-')+1, sectionId.length);
        
        $($("ul.sectionMenu li a")[sectionId - 1]).trigger('click');
    }); 
    
    setInterval(function()
    {
        News.loadNewsAnimate(500);
    }, 8000);
    
    $('ul.listBlocks li a').click(function(e)
    {
        e.preventDefault();
        
        var id = $(this).attr('href');
        var height = 0;
        var section = '#'+$(this).attr('rel');
        
        id = id.substring(1, id.length);
        
        $(section+" ul.listBlocks li a").removeClass('blockSelected');
        $(this).addClass('blockSelected');
        
        $(section+' div.blocksContainer div.blockContent').each(function(index, block)
        {
            if($(block).attr('id') == id) return false;
            
            if($(block).height() > 0)
                height += $(block).height() + 100;
        });
        
        $(section+" .blocksArea").animate({scrollTop: height}, 1000, "swing");
        
    });

    var contentHeight = $(".scrollContent").height();
    var scrollMax = contentHeight - $("#content").height();

    // Mouse scrolling
    $('.blocksArea').mousewheel(function(event, delta){
        if (delta > 0) {
            $(this).scrollTop( parseInt( $(this).scrollTop()) - 15 );
        } else {
            $(this).scrollTop( parseInt( $(this).scrollTop()) + 15 );
        }
    });
});

