var HEADER_HEIGHT = 127;
var animInProgress = false;
var activeSlideUrl = '';
var firstMenuAnimation = true;

$(document).ready(function() {    

    $.easing.def = "backEaseOut";
    	
    $(window).resize(function() {
        contentHeight = $(window).height() - HEADER_HEIGHT;
        if (contentHeight < 540)
        {
            contentHeight = 540;
        }
        contentWidth = $(window).width();
        if (contentWidth < 960)
        {
            contentWidth = 960;
        }
        
        $('.container').css('height', contentHeight);
        $('.container .slider').css('height', contentHeight);
        $('.container .panel').css('height', contentHeight);
        
        $('.container').css('width', contentWidth);
        $('.container .panel').css('width', contentWidth);
        
        $('.header-grad').css('width', contentWidth);
        
        $('.footer').css('width', contentWidth);
        
        resizePanelBackground();
    }).trigger('resize');
    
    activeSlideUrl = document.location.hash.substring(2);
    if (typeof disableAjaxLoad != "undefined" && disableAjaxLoad == true)
    {
        
    }
    else
    {
        if (activeSlideUrl == '')
        {
            activeSlideUrl = '/';
        }

        timestamp = new Date().getTime();
        url = activeSlideUrl + '?temp' + timestamp + '=' + timestamp;

        $.ajax({
        	url: url,  // can use the parameter 'id' to retrieve different dynamic return data
        	dataType: 'html',
        	success: function(html) {
        		$('.panel').html(html);
        	}
        });
    }
    
    $('a.slide-menu').live('click', function(e) {
	    e.preventDefault();

	    if ($('ul.main-menu li.active').index() == -1)
	    {
	        animationClick(this, 'horizontal-left');
	    }
	    else if ($(this).parent().index() < $('ul.main-menu li.active').index())
	    {
	        animationClick(this, 'horizontal-left');
	    }
	    else if ($(this).parent().index() > $('ul.main-menu li.active').index())
	    {
	        animationClick(this, 'horizontal-right');
	    }
	});
    
	$('a.slide-hor-left').live('click', function(e) {
	    e.preventDefault();
	    
	    animationClick(this, 'horizontal-left');
	});
	
	$('a.slide-hor-right').live('click', function(e) {
	    e.preventDefault();
	    
	    animationClick(this, 'horizontal-right');
	});
	
	$('a.slide-vert-down').live('click', function(e) {
	    e.preventDefault();
	    
	    animationClick(this, 'vertical-down');
	});
	
	$('a.slide-vert-up').live('click', function(e) {
	    e.preventDefault();
	    
	    animationClick(this, 'vertical-up');
	});
	
    $('.map a').die();
    $('.map a').live('mouseover', function() {
        if ($(this).hasClass('link-africe'))
        {            
            $('.map').addClass('map-africe');
        }
        else if ($(this).hasClass('link-north-america'))
        {  
            $('.map').addClass('map-north-america');
        }
        else if ($(this).hasClass('link-south-america'))
        {  
            $('.map').addClass('map-south-america');
        }
        else if ($(this).hasClass('link-australia'))
        {  
            $('.map').addClass('map-australia');
        }
        else if ($(this).hasClass('link-asia'))
        {  
            $('.map').addClass('map-asia');
        }
        else if ($(this).hasClass('link-europe'))
        {  
            $('.map').addClass('map-europe');
        }
    });
    $('.map a').live('mouseout', function() {
        $('.map').removeClass('map-africe');
        $('.map').removeClass('map-north-america');
        $('.map').removeClass('map-south-america');
        $('.map').removeClass('map-australia');
        $('.map').removeClass('map-asia');
        $('.map').removeClass('map-europe');
    });
    
	bindFocusBlur($('#newsletter-email'), 'Wpisz swój email...');
	
	$('.newsletter .button-add').click(function(e) {
	    e.preventDefault();
	    
	    email = $('#newsletter-email').val();
	    if (!validateEmail(email))
	    {
	        alert('Podaj poprawny adres email');
	        return false;
	    }
	    
	    $('body').append('<div class="loader"></div>');
	    
	    $.ajax({
            type: 'POST',
            url: '/newsletter/subscribe',
            data: { email: email },
            success: function(data) {
                
                $('.loader').remove(); 
                
                if (data.length > 0)
                {
                    alert(data);
                }
                
                $('#newsletter-email').val('Wpisz swój email...');
            }
        });
	});
	
	$('.newsletter .button-remove').click(function(e) {
	    e.preventDefault(); 
	    
	    email = $('#newsletter-email').val();
	    if (!validateEmail(email))
	    {
	        alert('Podaj poprawny adres email');
	        return false;
	    }
	    
	    $('body').append('<div class="loader"></div>');
	    
	    $.ajax({
            type: 'POST',
            url: '/newsletter/unsubscribe',
            data: { email: email },
            success: function(data) {
                
                $('.loader').remove(); 
                
                if (data.length > 0)
                {
                    alert(data);
                }
                
                $('#newsletter-email').val('Wpisz swój email...');
            }
        });
	});
});

function animationClick(elem, type)
{
    link = $(elem).attr('href');
    link = link.substring(link.indexOf('#') + 2);
    
    if (activeSlideUrl == link || animInProgress)
    {
        return false;
    }
    
    activeSlideUrl = link;
    animInProgress = true;
    
    setPanelSize(type);
    
    $('body').append('<div class="loader"></div>');
    pageLoaded = 0;
    
    timestamp = new Date().getTime();
    url = activeSlideUrl + '?temp' + timestamp + '=' + timestamp;
    
    $.ajax({
		url: url,  // can use the parameter 'id' to retrieve different dynamic return data
		dataType: 'html',
		success: function(html) {		

            $('.container .panel').after('<div class="panel">' + html + '</div>');
            
            $('.container .panel').css('height', $(window).height() - HEADER_HEIGHT);
            $('.container .panel').css('width', $(window).width()); 
            
            document.title = $('.container .panel:last .pagetitle').text();
            //$('.container .panel:last .pagetitle').remove();
            
            pageLoaded++;
            $(window).trigger('resize');
            
            $('.container .panel img.panel-background').load(function() {
                pageLoaded++;  
                $(window).trigger('resize');
                if (pageLoaded == 2)
                {
                    if (type == 'horizontal-left')
                    {
                        animateHorizontalLeft(activeSlideUrl);
                    }
                    else if (type == 'horizontal-right')
                    {
                        animateHorizontalRight(activeSlideUrl);
                    }
                    else if (type == 'vertical-up')
                    {
                        animateVerticalUp(activeSlideUrl);
                    }
                    else if (type == 'vertical-down')
                    {
                        animateVerticalDown(activeSlideUrl);
                    }
                }
            });
            
            if (pageLoaded == 2)
            {
                if (type == 'horizontal-left')
                {
                    animateHorizontalLeft(activeSlideUrl);
                }
                else if (type == 'horizontal-right')
                {
                    animateHorizontalRight(activeSlideUrl);
                }
                else if (type == 'vertical-up')
                {
                    animateVerticalUp(activeSlideUrl);
                }
                else if (type == 'vertical-down')
                {
                    animateVerticalDown(activeSlideUrl);
                }
            }
		}
	});
}

function animateHorizontalLeft(url)
{
    $('.loader').remove(); 
    
    leftPos = $(document).width() * (-1);
    $('.container .slider').stop().animate({
        'left': leftPos
    }, {
        //queue: false,
        duration: 1500,
        easing: "backEaseOut",
        complete: function() {                        
            $('.container .panel:first').remove();
            $('.container .slider').css('left', '0px');
            animInProgress = false;
            document.location.hash = '!' + url;
            $(window).trigger('resize');
            $('.reference-photos a').fancybox();
            $('a.fancybox').fancybox();
        }
    });
}

function animateHorizontalRight(url)
{
    $('.loader').remove(); 
    
    $panel = $('.container .panel:last').clone();
    $('.container .panel:last').remove();
    $('.container .panel:first').before($panel)
    $('.container .slider').css('left', -($(document).width()));
    
    //leftPos = $(document).width() * (-1);
    $('.container .slider').stop().animate({
        'left': 0
    }, {
        //queue: false,
        duration: 1500,
        easing: "backEaseOut",
        complete: function() {                        
            $('.container .panel:last').remove();
            $('.container .slider').css('left', '0px');
            animInProgress = false;
            document.location.hash = '!' + url;
            $(window).trigger('resize');
            $('.reference-photos a').fancybox();
            $('a.fancybox').fancybox();
        }
    });
}

function animateVerticalUp(url)
{
    $('.loader').remove(); 
                    
    topPos = ($(document).height() - HEADER_HEIGHT) * (-1);
    $('.container .slider').stop().animate({
        'top': topPos
    }, {
        //queue: false,
        duration: 1500,
        easing: "backEaseOut",
        complete: function() {                        
            $('.container .panel:first').remove();
            $('.container .slider').css('top', '0px');
            animInProgress = false;
            document.location.hash = '!' + url;
            $(window).trigger('resize');
            $('.reference-photos a').fancybox();
            $('a.fancybox').fancybox();
        }
    });
}

function animateVerticalDown(url)
{
    $('.loader').remove(); 
    
    $panel = $('.container .panel:last').clone();
    $('.container .panel:last').remove();
    $('.container .panel:first').before($panel)
    
    topPos = ($(document).height() - HEADER_HEIGHT) * (-1);
    $('.container .slider').css('top', topPos);
    
    $('.container .slider').stop().animate({
        'top': 0
    }, {
        //queue: false,
        duration: 1500,
        easing: "backEaseOut",
        complete: function() {                        
            $('.container .panel:last').remove();
            $('.container .slider').css('top', '0px');
            animInProgress = false;
            document.location.hash = '!' + url;
            $(window).trigger('resize');
            $('.reference-photos a').fancybox();
            $('a.fancybox').fancybox();
        }
    });
}

function setPanelSize(type)
{
    if (type == 'horizontal-left' || type == 'horizontal-right')
    {
        $('.container .slider').css('height', $(window).height() - HEADER_HEIGHT);
	    $('.container .slider').css('width', '10000px');
    }
    else if (type == 'vertical-up' || type == 'vertical-down')
    {
        $('.container .slider').css('height', '10000px');
	    $('.container .slider').css('width', $(window).width());
    }
}

function resizePanelBackground()
{
    $('.container .panel img.panel-background').load(function() {
        //$('.container .panel img').css('height' $(window).height() - 127);
        $('.container .panel img.panel-background').css('width', $(window).width());     
    });
    //$('.container .panel img').css('height', $(window).height() - 127);
    $('.container .panel img.panel-background').css('width', $(window).width());
}

function getCurrentMainMenuElement()
{
    index = null;
    if ($('ul.main-menu li.active').length > 0)
    {
        index = $('ul.main-menu li.active').index();
    }
    return index;
}

function setMainMenu(elemClass)
{
    $('.main-menu li').removeClass('active');
    $('.main-menu li.' + elemClass).addClass('active');
    
    /*startElem = getCurrentMainMenuElement();
    if (startElem == null)
    {
        startElem = -1;
    }
    
    $('.main-menu li').removeClass('active');
    if (elemClass != 'home')
    {
        $('.main-menu li.' + elemClass).addClass('active');
    }
    
    endElem = getCurrentMainMenuElement();
    if (endElem == null)
    {
        endElem = -1;
    }

    if (firstMenuAnimation == false)
    {
        $('ul.main-menu li a span').addClass('no-bg');
        $('ul.main-menu').addClass('show-bg');
        $('ul.main-menu').css('background-position', (startElem * 86 + 8) + 'px 106px');
        
        $('ul.main-menu').animate({
            'background-position': (endElem * 86 + 8) + 'px 106px'
        }, {
            //queue: false,
            duration: 1000,
            easing: "backEaseOut",
            complete: function() {                        
                $('ul.main-menu').removeClass('show-bg');
                $('ul.main-menu li a span').removeClass('no-bg');
            }
        });
    }
    
    firstMenuAnimation = false;*/
}

function validateEmail(email) 
{
   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   if(reg.test(email) == false) 
   {
      return false;
   }
   return true;
}

function bindFocusBlur($field, value)
{
    $field.die();
    $field.live('focus', function() {
	    if ($(this).val() == value)
	    {
	        $(this).val('');
	    } 
	});
	
	$field.live('blur', function() {
	    if ($(this).val() == '')
	    {
	        $(this).val(value);
	    } 
	});
}

