$(document).ready(function(){

	var hiLightWidth = 300;
    var hiLightSpeed = 250;

    $('#total').width( $('.widthMeter').width() );

    var isHomePage = true;
	var isChangingPage = false;
	
    $('.jumpToSubpage').live('click', function(){
	
		if ( isChangingPage ){
			return false;
		}
		isChangingPage = true;
	
        if ( $(this).parents('.blockLinks').size() ){
            return false;
        }

        if ( isHomePage ){
            $('#contentsRail').animate({
                'margin-left' : '-1890px'
            }, 750);
        }
        isHomePage = false;

        var linkAddress = $(this).attr('href');
        linkAddress = linkAddress.replace('#','');
        var linkParts = linkAddress.split('?');

        var page = '';
        var data = '';

        if ( '' != linkParts[1] ){
            page = linkParts[0];
            data = linkParts[1];
        } else {
            page = linkParts;
        }

        var urlPrefix = 'pages/';
        var fullUrl = urlPrefix + page;

        if ( $.browser.msie ){

            $('#subpageLoader').show();
            $('#subpageContainer').hide();

            loadPage( fullUrl, data )

        } else {

            $('#subpageLoader').fadeIn('slow');
            $('#subpageContainer').fadeOut('slow', function(){
                loadPage( fullUrl, data )
            });

        }

        return false;

    });



    function loadPage( fullUrl, data ){
        $.ajax({
            url : fullUrl,
            data : data,
            success : function( response ){
                setTimeout( function(){
                    $('#subpageLoader').hide();
                    $('#subpageContainer').html(response);
                    if ( $.browser.msie ){
                        $('#subpageContainer').show();
                    } else {
                        $('#subpageContainer').fadeIn('500');
                    }
                    filterContents();
					isChangingPage = false;
                }, 200 );
            },
			error: function(){
				isChangingPage = false;
			}
        });
    }



    $('.backToHome, #mainMenu .home').live('click', function(){
        $('#mainMenu li').removeClass('active').removeClass('blockLink')
		.find('.hiLight').css({
			'backgroundPosition' : '-' + hiLightWidth + 'px 0px'
		});
        $('#contentsRail').animate({
            'margin-left' : '0px'
        }, 600);
        isHomePage = true;
		return false;
    });



    /**
     *
     */
    $('.weekDaysList li:eq(0)').addClass('active');

    var weekDaysFunction = function(){
        $('.weekDaysList li').removeClass('active');

        $(this).addClass('active');

        var thisIndex = $(this).index();

		var newMargin = 263 * thisIndex;
		newMargin = '-' + newMargin + 'px';
		
		$('#weekEvents').stop( true );
		$('#weekEvents').animate({'margin-top' : newMargin}, 600);
		
		/*
		
        $('#weekEvents li').hide();

        $('#weekEvents li:eq('+thisIndex+')').show();
		
		*/
		
    }

    $('.weekDaysList li').hover( weekDaysFunction ).click( weekDaysFunction );

    var doAutoSlide = true;
    $('#weekEvents, .weekDaysList').hover(function(){
        doAutoSlide = false;
    }, function(){
        doAutoSlide = true;
    });

    setInterval( function(){

        if ( !doAutoSlide || !isHomePage ){
            return false;
        }

        var currentItemIndex = $('.weekDaysList li.active').index('.weekDaysList li');
        var nextItemIndex = 0;

        if ( currentItemIndex != ($('.weekDaysList li').size()-1) ){
            nextItemIndex = currentItemIndex + 1;
        }
		
		if ( $('#weekEvents li:eq(' + nextItemIndex + ')').has('img') ){
			var testImage = new Image();
			testImage.src = $('#weekEvents li:eq(' + nextItemIndex + ') img').attr('src');
			if ( !testImage.complete ){
				return false;
			}
		}

        $('.weekDaysList li:eq('+nextItemIndex+')').trigger('click');

    }, 3000 );


    /**
     * GALERIA
     */
    $('.galleryNavigation').live('click', function(){

        var rail = $('.galleryRail');
        var currentPage = $('.galleryRail ul.visible').index();
        var nextPage = currentPage;

        if ( $(this).hasClass('galleryNext') ){
            nextPage++;
        } else {
            nextPage--;
        }

        if ( nextPage < 0 ){
            return false;
        }

        if ( (nextPage+1) > $('.galleryRail ul').size() ){
            return false;
        }

        rail.find('ul.visible').removeClass('visible');
        rail.find('ul:eq('+nextPage+')').addClass('visible');

        var newMargin = nextPage * 585;
        newMargin = '-' + newMargin + 'px';

        rail.animate({
            'margin-left' : newMargin
        });

    })



    $('.showGallery').live('click', function(){

        if ( $(this).parent().hasClass('active') ){
            return false;
        }

        var galleryUrlParts = $(this).attr('href').split('#');
        var galleryName = galleryUrlParts[1];
        var galleryStringId = '#hiddenGallery-' + galleryName;

        $('.galleriesList li').removeClass('active');
        $(this).parent().addClass('active');

        $('.galleryRail').fadeOut( 250, function(){
            $('.galleryRail').html( $(galleryStringId).html() );
            $('.galleryRail').fadeIn( 250 );
            filterContents();
        });
		
		$('.pageHeading.galeria').text( $(this).text() );

        return false;

    });



    /**
     *
     */
    $('#mainMenu li').hoverIntent({

        over : function(){

            if ( $(this).hasClass('active') ){
                return false;
            }

            var hiLightMargin = hiLightWidth - $(this).width();

            $(this).find('.hiLight').animate({
                'backgroundPosition' : '-' + hiLightMargin + 'px 0px'
            }, hiLightSpeed);

        },

        out : function(){

            if ( $(this).hasClass('active') ){
                return false;
            }

            $(this).find('.hiLight').animate({
                'backgroundPosition' : '-'+hiLightWidth+'px 0px'
            }, hiLightSpeed);

        },

        timeout : 10

    });



    $('#mainMenu li').bind('click', function(){
	
		if ( isChangingPage ){
			return false;
		}

        if ( $(this).hasClass('active') ){
            return false;
        }

        $('#mainMenu li .hiLight').css({
            'backgroundPosition' : '-' + hiLightWidth + 'px 0px'
        });

        $('#mainMenu li').removeClass('active').removeClass('blockLink');

        if ( !$(this).hasClass('blockActivity') ){
            $(this).addClass('active').addClass('blockLink');
        }

    });
	
	
	
	if ( showVideoIntro ){
		$('#videoIntro').show().addClass('visiblePopupItem');
		if ($.browser.msie && $.browser.version.substr(0,1)==7) {
			var videoClone = $('#videoInto .centerizeMe').html();
			$('#videoInto .centerizeMe object').remove();
			$('#videoInto .centerizeMe').html(videoClone);
		}
		centerizeMe();
	}
	
	if ( showEventPopup ){
		if ( $('#videoIntro').hasClass('visiblePopupItem') ){
			$('#eventPopup').addClass('waitingPopupItem');
		} else {
			$('#eventPopup').addClass('visiblePopupItem');
			$('#eventPopup').show();
			centerizeMe();
		}
	}
	
	if ( !showVideoIntro && !showEventPopup ){
		$('#popupOverall').hide();
	}
	
	$('#popupOverall, .closePopup').live('click', function(){
		popupClosing();
	});
		
	$('.socialBabe .rss').hover(function(){
		$('.socialBabe').addClass('rssHover');
	}, function(){
		$('.socialBabe').removeClass('rssHover');
	});
	
	$('.socialBabe .facebook').hover(function(){
		$('.socialBabe').addClass('facebookHover');
	}, function(){
		$('.socialBabe').removeClass('facebookHover');
	});
	
	
});

function centerizeMe(){

	var theWindow = $(window);
	var windowWidth = theWindow.width();
	var windowHeight = theWindow.height();

	$('.centerizeMe').each(function(){
		
		var box = $(this);
		var boxWidth = box.width();
		var boxHeight = box.height();
								
		var topMargin = Math.floor( ( ( windowHeight - boxHeight ) / 2 ) );
		var leftMargin = Math.floor( ( ( windowWidth - boxWidth ) / 2 ) );
		
		box.css({
			'left' : leftMargin + 'px',
			'top' : topMargin + 'px'
		});
		
	});
	
}

function filterContents(){
    
    $('#subpageContainer .theScroller').jScrollPane({
        showArrows : true,
        scrollbarWidth : 29,
        dragMinHeight : 28,
        dragMaxHeight : 28
    });

    $('.lightbox').lightbox({
        fitToScreen:true
    });

    $('.galleryRail').css({
        'margin-left' : '0px',
        'left' : '0px'
    });

    $('.galleryRail ul:eq(0)').addClass('visible');

    if ( $('.galleriesList li').size() && !$('.galleriesList li.active').size() ){
        $('.galleriesList li:eq(0) a').trigger('click');
    }
	
	if ( $('.totalGalleries').size() && !$('.galleriesPagination li').size() ){
				
		var galleriesCount = $('.totalGalleries .galleriesRail ul').size();	
		
		$('.galleriesPagination').html('');
		for ( i=1; i<=galleriesCount; i++ ){
			var newGalleryItem = $('<li><a rel="'+i+'">'+i+'</a></li>');
			$('.galleriesPagination').append( newGalleryItem );
		}
		$('.galleriesPagination li:eq(0)').addClass('active');
		
		$('.galleriesPagination li a').live('click', function(){
						
			var listItem = $(this).parent();
			var listItemIndex = listItem.index();
			
			if ( listItem.hasClass('active') ){
				return false;
			}
			
			$('.galleriesPagination li').removeClass('active');
			
			listItem.addClass('active');
			
			var stepWidth = $('.totalGalleries').width();
			stepWidth = listItemIndex * stepWidth;
			stepWidth = '-' + stepWidth + 'px';
			
			$('.galleriesRail').animate({
				'left' : stepWidth
			}, 450);
		
		});
		
	}

}


function popupClosing(){
	$('#popupOverall').hide();
	$('.visiblePopupItem').hide().removeClass('visiblePopupItem');
	
	if ( $('.waitingPopupItem').size() ){
		$('.waitingPopupItem:eq(0)').show().removeClass('waitingPopupItem').addClass('visiblePopupItem');
		$('#popupOverall').show();
		centerizeMe();
	}	
}

function onEndMovie(){
	popupClosing();
}





