/******************************
  GLOBAL VARIABLES
*******************************/
var animationInProgress = false;
var activeSlideUrl = '';
var sideMenuDisabled = false;
var animationTime = 500;
var animationEasing = 'easeInCubic';

document.onkeydown = function(e) {
    var k = e.keyCode;
    if(k == 37 || k == 39) {
        return false;
    }
}

/******************************
  HEADER MENU
*******************************/
$(document).ready(function() {

    $('ul.main-menu > li.with-submenu').each(function() {
        
        var menuWidth = 0;
    
        $(this).find('> .menu-container > ul > li').each(function() {
            menuWidth += parseInt($(this).outerWidth());
        });
        
        $(this).find('> .menu-container').width(menuWidth);
        $(this).find('> .menu-container > ul').width(menuWidth).css('left', '-' + menuWidth + 'px');
    });

    $('.main-menu li a.expand').live('click', function(e) {
        e.preventDefault();
        
        if ($(this).hasClass('active'))
        {
            $(this).removeClass('active');
            $(this).parent().find('> .menu-container > ul').animate({
                left: (-1) * parseInt($(this).parent().find('> .menu-container').width())
            });
        }
        else
        {
            $(this).addClass('active');
            $(this).parent().find('> .menu-container > ul').animate({
                left: 0
            });
        }
    });
});

/******************************
  HEADER SEARCH
*******************************/
$(document).ready(function() {
    $('.header-social .search .search-box a').click(function(e) {
        e.preventDefault();
        
        if ($(this).parent().hasClass('active'))
        {
            $('.header-social .search .search-box').animate({
                width: 24
            });
            $(this).parent().removeClass('active');
        }
        else
        {
            $('.header-social .search .search-box').animate({
                width: 144
            });
            $(this).parent().addClass('active')
        }
    });

    $('.header-social .search .search-box div input').focus(function() {
        if ($(this).val() == 'Szukaj...')
        {
            $(this).val('');
        }
    });
    $('.header-social .search .search-box div input').blur(function() {
        if ($(this).val() == '')
        {
            $(this).val('Szukaj...');
        }
    });
});

/******************************
  PAGE FOLDING CORNER
*******************************/
$(document).ready(function() {
    $(".bg-corner").hover(function() { //On hover...
        $(".bg-corner a span.qr-code").stop().animate({
            width: '418px',
            height: '418px'
        }, 500);
        $(".bg-corner img").stop().animate({
            width: '418px',
            height: '418px'
        }, 500);
    } , function() {
        $(".bg-corner img").stop().animate({
            width: '50px',
            height: '50px'
        }, 220);
        $(".bg-corner a span.qr-code").stop().animate({
            width: '20px',
            height: '20px'
        }, 200);
    });
});

/******************************
  LOAD MAIN PAGE / SWITCH PAGE
*******************************/
$(document).ready(function() {
    /*if (window.location.pathname != '/')
    {
        window.location = '/#!' + window.location.pathname;
    }*/

    loadFirstPage();
    
    $('.page-switch').live('click', function(e) {
        e.preventDefault();
        
        switchPage(this);
		
		return false;
    });
    
    $('body').bind('loaderHaveFinished', function(e) {
        newHeight = getContentHeight();
        
        $('.bg-image').animate({
            left: 0
        }, { 
            duration: animationTime, 
            easing: animationEasing
        });
        $('.bg-mask').animate({
            left: 0
        }, { 
            duration: animationTime, 
            easing: animationEasing
        });
        
        $('.content').animate({
            height: newHeight
        }, animationTime, function() {
        
            $('.content').css('overflow', 'visible');
			
            animationInProgress = false;
            window.location.hash = '!' + activeSlideUrl;
            
            $(window).trigger('resize');
            $('.side-header').removeClass('active');
            $(window).trigger('scroll');
            
            $('body').trigger('afterPageSwitch');

            reloadHeight();
        });
    });    
});

$(window).resize(function() {
    newContentHeight = getContentHeight();
    $('.content').height(newContentHeight);
    
    bgHeight = getBackgroundHeight();    
    $('.bg-mask').height(bgHeight);
    $('.bg-image').height(bgHeight);
    $('.bg-triangle').height(bgHeight);
});

function loadFirstPage()
{
    activeSlideUrl = window.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) {
                $('.content').css('overflow', 'hidden');
                $('.content .content-inside').html('');
        		$('.content .content-inside').html(html);                
                
				if ($('.content .content-inside .title-box h1').length > 0)
				{
					var headerHeight = parseInt($('.header').outerHeight());
					var scrollTop = parseInt($(window).scrollTop());
					
					if (scrollTop < headerHeight)
					{
						$('html,body').animate({
							scrollTop: headerHeight
						}, animationTime);
					}
				}
				
                newHeight = getContentHeight();
                
                document.title = $('.content .content-inside .meta-pagetitle').text();
                $('.content .content-inside .meta-pagetitle').remove();
                
                var newImage = $('.content .content-inside .meta-bgimage').attr('src');
                $('.content .content-inside .meta-bgimage').remove();
                
                $('<img />').attr('src', newImage).load(function() {

                    $('.bg-image img').attr('src', newImage);
                    $('.bg-image').animate({
                        left: 0
                    }, { 
                        duration: animationTime, 
                        easing: animationEasing
                    });
                    $('.bg-mask').animate({
                        left: 0
                    }, { 
                        duration: animationTime, 
                        easing: animationEasing
                    });
                    
                    $('.content').animate({
                        height: newHeight
                    }, animationTime, function() {
                        
                        $('.content').css('overflow', 'visible');
                    
                        animationInProgress = false;
                        window.location.hash = '!' + activeSlideUrl;
                        
                        $(window).trigger('resize');
                        $('.side-header').removeClass('active');
                        $('html,body').trigger('scroll');
                        
                        $('body').trigger('afterPageSwitch');
                        
                        reloadHeight();
                    });
                });
            }
        });
    }
}

function switchPage(elem)
{
    link = $(elem).attr('href');
    link = link.substring(link.indexOf('#') + 2);

    if (activeSlideUrl == link || animationInProgress)
    {
        return false;
    }
    
    activeSlideUrl = link;
    animationInProgress = true;
    
    timestamp = new Date().getTime();
    url = activeSlideUrl + '?temp' + timestamp + '=' + timestamp;
    
    $('.bg-image').animate({
        left: -332
    }, { 
        duration: animationTime, 
        easing: animationEasing
    });
    $('.bg-mask').animate({
        left: -332
    }, { 
        duration: animationTime, 
        easing: animationEasing
    });
    $('.content').css('overflow', 'hidden').animate({
        height: 0
    }, animationTime, function () {    
    
        animateLoading();
    
        $.ajax({
            url: url,  // can use the parameter 'id' to retrieve different dynamic return data
            dataType: 'html',
            success: function(html) {		

                $('.content .content-inside').html('');
                $('.content .content-inside').html(html);
                
                newHeight = getContentHeight();
                
                document.title = $('.content .content-inside .meta-pagetitle').text();
                $('.content .content-inside .meta-pagetitle').remove();
                
                var newImage = $('.content .content-inside .meta-bgimage').attr('src');
                $('.content .content-inside .meta-bgimage').remove();

                $('<img />').attr('src', newImage).load(function() {
                
                    $('.bg-image img').attr('src', newImage);
                
                    finalizeLoading();
                });
            }
        });
    });
}

var loadingTimer = null;
var loadingFinished = false;

function animateLoading()
{
    loadingFinished = false;

    clearTimeout(loadingTimer);
    
    $('body .loading').css({
        width: 0,
        height: 16
    }).show();
    
    $('body .loading .loading-counter').text('0%');
    
    loadingTimer = setTimeout(stepLoading, 40);
}

function stepLoading()
{
    clearTimeout(loadingTimer);
    
    newWidth = $('body .loading').width();
    newWidth += 16;
    
    $('body .loading').width(newWidth);
    $('body .loading .loading-counter').text(parseInt(100*newWidth/720) + '%');

    
    if (newWidth < 690)
    {
        loadingTimer = setTimeout(stepLoading, 40);
    }
    else
    {
        loadingFinished = true;
    }
}

function finalizeLoading()
{
    loadingFinished = false;

    clearTimeout(loadingTimer);
    
    $('body .loading').width(700);
    $('body .loading .loading-counter').text('100%');
    
    $('body .loading').delay(200).animate({
        height: 0
    }, 200, function() {
        $('body').trigger('loaderHaveFinished');
    });
}

function reloadHeight()
{
    /*if ($('.text-content').length > 0)
    {
        textHeight = $('.text-content-inside').height();
        $('.text-content').animate(textHeight);
    }

    newHeight = getContentHeight();
    
    $('.content').animate({
        height: newHeight
    });*/
    
    $('.text-content').css('height', 'auto');
    $('.content').css('height', 'auto');
}

function loadListPage(link)
{
    link = link.substring(link.indexOf('#') + 2);

    if (activeSlideUrl == link || animationInProgress)
    {
        return false;
    }
    
    activeSlideUrl = link;
    animationInProgress = true;
    
    timestamp = new Date().getTime();
    url = activeSlideUrl + '?temp' + timestamp + '=' + timestamp + '&textcontent=1';
    
    $.ajax({
		url: url,  // can use the parameter 'id' to retrieve different dynamic return data
		dataType: 'html',
		success: function(html) {		

            startHeight = parseInt($('.list-with-slider').outerHeight()) + parseInt($('.title-box').outerHeight());
        
            $('.content').css('height', 'auto');
        
            var headerHeight = parseInt($('.header').outerHeight());
            var scrollTop = parseInt($(window).scrollTop());
            
            if (scrollTop < headerHeight)
            {
                $('html,body').animate({
                    scrollTop: headerHeight
                }, animationTime);
            }
        
            $('.text-content').animate({
                height: 0
            }, animationTime, function() {
            
                $('.text-content-inside').html('');
                $('.text-content-inside').html(html);
                
                galleryInit();
                
                textHeight = $('.text-content-inside').height();
                
                newHeight = getContentHeight() + textHeight;
                
                document.title = $('.content .content-inside .meta-pagetitle').text();
                $('.content .content-inside .meta-pagetitle').remove();
                
                $('.text-content').animate({
                    height: textHeight
                }, animationTime, function() {
                    animationInProgress = false;
                    window.location.hash = '!' + activeSlideUrl;
                    
                    $('.content').css('height', newHeight);
                    
                    $(window).trigger('resize');
                    $('.side-header').removeClass('active');
                    $('html,body').trigger('scroll');
                    
                    reloadHeight();
                });
            });
		}
	});
}

function getContentHeight()
{
    windowHeight = $(window).height();
    headerHeight = $('.header').outerHeight();
    footerHeight = $('.footer').outerHeight();
    contentHeight = $('.content-inside').outerHeight();

    sum = headerHeight + footerHeight + contentHeight;
    diff = windowHeight - sum; 
    
    if (diff > 0)
    {
        contentHeight += diff;
    }
    
    return contentHeight;
}

function getBackgroundHeight()
{
    windowHeight = $(window).height();
    headerHeight = $('.header').outerHeight();
    footerHeight = $('.footer').outerHeight();
    contentHeight = $('.content').outerHeight();
    
    documentHeight = headerHeight + footerHeight + contentHeight;
    
    if (documentHeight > windowHeight)
    {
        return documentHeight;
    }
    
    return windowHeight;
}


/******************************
  SIDE MENU
*******************************/
$(document).ready(function() {

    $('.side-top').click(function() {
        $('html,body').animate({
            scrollTop: 0
        }, 1000);
    });
    $('.side-top').hover(function() {
        $(this).stop().animate({
            height: 80
        });
        $(this).find('span').stop().animate({
            top: 5
        });
    }, function() {
        $(this).stop().animate({
            height: 65,
            backgroundPosition: '(0px 10px)'
        });
        $(this).find('span').stop().animate({
            top: 10
        });
    });

    $(window).scroll(function() {
    
        var headerHeight = 640;//$('.header').outerHeight();    
        var scrollTop = $(window).scrollTop();
        
        if (sideMenuDisabled == false)
        {
            if (scrollTop > headerHeight && !$('.side-header').hasClass('active'))
            {
                if ($('.page-menu').length > 0)
                {
                    sideHeight = parseInt($('.side-header').outerHeight()) + parseInt($('.side-header').css('top')) + 20;
                    pageMenuHeight = parseInt($('.page-menu').outerHeight());
                    
                    $('.side-top').css('top', (pageMenuHeight + sideHeight + 20));
                    
                    if ($('.page-menu').hasClass('page-menu-hidden'))
                    {
                        $('.page-menu').css('top',sideHeight).removeClass('page-menu-top').animate({
                            left: 50
                        });
                    }
                    else
                    {
                        $('.page-menu').css('position', 'fixed').removeClass('page-menu-top').animate({
                            top: sideHeight
                        });
                    }
                }
                else
                {
                    sideHeight = parseInt($('.side-header').outerHeight()) + parseInt($('.side-header').css('top')) + 20;
                    
                    $('.side-top').css('top', sideHeight);
                }
                
                $('.side-top').animate({
                    left: 50
                });
                
                $('.side-header').animate({
                    left: 50
                });
                $('.side-header').addClass('active');
            }
            if (scrollTop < headerHeight && $('.side-header').hasClass('active'))
            {
                if ($('.page-menu').hasClass('page-menu-hidden'))
                {
                    $('.page-menu').removeClass('page-menu-top').animate({
                        left: -210
                    });
                }
                else
                {
                    $('.page-menu').css('position', 'absolute').addClass('page-menu-top').animate({
                        top: 112
                    });
                }
 
                $('.side-top').animate({
                    left: -210
                });
            
                $('.side-header').animate({
                    left: -210
                });
                $('.side-header').removeClass('active')
            }
        }
        
        if (scrollTop > 465)
        {
            $('.bg-mask').addClass('fixed');
            $('.bg-image').addClass('fixed');
            $('.bg-triangle').addClass('fixed');
        }
        if (scrollTop < 465)
        {
            $('.bg-mask').removeClass('fixed');
            $('.bg-image').removeClass('fixed');
            $('.bg-triangle').removeClass('fixed');
        }
    });
});


/******************************
  FOOTER TOP LINK
*******************************/
$(document).ready(function() {
    $('.footer-top-link').click(function(e) {
        e.preventDefault();
        
        $('html,body').animate({
            scrollTop: 0
        }, animationTime);
    });

    $('.footer-top-link').hover(function() {
        $(this).stop().animate({
            height: 80
        });
        $(this).find('span').stop().animate({
            top: 5
        });
    }, function() {
        $(this).stop().animate({
            height: 65
        });
        $(this).find('span').stop().animate({
            top: 10
        });
    });
});

/******************************
  ACTIVATE MENU
*******************************/

function menuSelect(index)
{
    $('.main-menu > li').removeClass('active');   
    $('.side-menu > li').removeClass('active');
    
    if (index != 'home')
    {
        $('.main-menu > li').eq(index).addClass('active');
        $('.side-menu > li').eq(index).addClass('active');
    }
}

/******************************
  GALLERIES
*******************************/

function galleryInit()
{
    $('.gallery').each(function() {
        
        $gallery = $(this);
    
        photoFirstHeight = $gallery.find('.photo:first img').height();
        
        $gallery.find('.photos-wrap .next').height(parseInt(photoFirstHeight/2));    
        $gallery.find('.photos-wrap .prev').height(parseInt(photoFirstHeight/2));
        
        $gallery.find('.photos-wrap, .navigation').height(photoFirstHeight);
        
        if (photoFirstHeight < 50)
        {
            $gallery.find('.photo:first img').load(function() {
            
                photoFirstHeight = this.height;
                
                $gallery.find('.photos-wrap .next').height(parseInt(photoFirstHeight/2));    
                $gallery.find('.photos-wrap .prev').height(parseInt(photoFirstHeight/2));
        
                $gallery.find('.photos-wrap, .navigation').height(photoFirstHeight);
            });
        }

        $gallery.find('.navigation ul li:first').addClass('active');
        $gallery.find('.desc').html('');
        if ($gallery.find('.photo').eq(0).find('img').attr('alt') != '')
        {
            $gallery.find('.desc').html('<div class="inside">' + $gallery.find('.photo').eq(0).find('img').attr('alt') + '</div>');
        }
        
        $gallery.find('.navigation ul li a').click(function(e) {
            e.preventDefault();
        
            galleryShowPhoto($(this), $(this).parent().index());
        });
        
        $gallery.find('.navigation .prev, .photos-wrap .prev').hide();
        
        $gallery.find('.navigation .prev, .photos-wrap .prev').click(function(e) {
            e.preventDefault();
        
            $gallery = $(this).closest('.gallery');            
            index = $gallery.find('.navigation ul li.active').index();
            
            if (index <= 0)
            {                
                return;
            }
            
            $gallery.find('.navigation .next, .photos-wrap .next').show();
            
            index--;
            
            if (index <= 0)
            {
                $gallery.find('.navigation .prev, .photos-wrap .prev').hide();
            }
        
            galleryShowPhoto($(this), index);
        });
        
        $gallery.find('.navigation .next, .photos-wrap .next').click(function(e) {
            e.preventDefault();
        
            $gallery = $(this).closest('.gallery');
            index = $gallery.find('.navigation ul li.active').index();
            count = $gallery.find('.navigation ul li').length;
            
            if (index >= count - 1)
            {                
                return;
            }
            
            $gallery.find('.navigation .prev, .photos-wrap .prev').show();
            
            index++;
            
            if (index >= count - 1)
            {
                $gallery.find('.navigation .next, .photos-wrap .next').hide();
            }
        
            galleryShowPhoto($(this), index);
        });
    });
}

function galleryShowPhoto($el, index)
{
    $gallery = $el.closest('.gallery');

    $gallery.find('.navigation ul li').removeClass('active');
    $gallery.find('.navigation ul li').eq(index).addClass('active');
    
    photoHeight = $gallery.find('.photo').eq(index).find('img').height();
    $gallery.find('.photos-wrap, .navigation').animate({
        height: photoHeight
    });
    
    $gallery.find('.photos-wrap .next').height(parseInt(photoHeight/2));    
    $gallery.find('.photos-wrap .prev').height(parseInt(photoHeight/2));
    
    position = $gallery.find('.photo').eq(index).position();
    
    $gallery.find('.photos').animate({
        top: -position.top
    });
    
    $gallery.find('.desc').html('');
    if ($gallery.find('.photo').eq(index).find('img').attr('alt') != '')
    {
        $gallery.find('.desc').html('<div class="inside">' + $gallery.find('.photo').eq(index).find('img').attr('alt') + '</div>');
    }
}

