/**
 * Default
 */

function geoSizePage() {
	var ht = $(window).height();
    if (ht > 673) {
    	$('#home').css('padding-bottom', (ht - 673 + 300)+'px');
    	$('#detail').css('padding-bottom', (ht - 673 + 80)+'px');
    } else {
    	$('#home').css('padding-bottom', '300px');
    	$('#detail').css('padding-bottom', '80px');
    }
}

var photoGalleryCurIndex = 0;
var photoGalleryCount = 0;
function prepPhotoGallery() {
	photoGalleryCount = $('#photoGallery div.image').length;
	$('#photoGallery .meta .count').empty().append( photoGalleryCount );
	
	$('#photoGallery a.close').click(function(){
		/*		// reset to first item
		photoGalleryCurIndex = 0;
		$('#photoGallery .lining').scrollTo( $('#photoGallery .inner div.image:eq(0)') , {duration:0, axis:"x"});
		$('#photoGallery .meta .current').empty().append( '1' );
		*/
		$('#photoGallery').fadeOut('fast', function(){
			$('#screenImages').fadeIn('fast');
    	});
	});
	
	$('#photoGallery a.next').click(function(){
		if (photoGalleryCurIndex == photoGalleryCount-1) {
			photoGalleryCurIndex = 0;
		} else {
			photoGalleryCurIndex++;
		}
		
		$('#photoGallery .lining').scrollTo( $('#photoGallery .inner div.image:eq('+photoGalleryCurIndex+')') , {duration:700, axis:"x"});
		$('#photoGallery .meta .current').empty().append( photoGalleryCurIndex+1 );
	});
	$('#photoGallery a.previous').click(function(){
		if (photoGalleryCurIndex == 0) {
			photoGalleryCurIndex = photoGalleryCount-1;
		} else {
			photoGalleryCurIndex--;
		}
		
		$('#photoGallery .lining').scrollTo( $('#photoGallery .inner div.image:eq('+photoGalleryCurIndex+')') , {duration:700, axis:"x"});
		$('#photoGallery .meta .current').empty().append( photoGalleryCurIndex+1 );
	});
	
	prepPhotoGallery2();
}

var photoGallery2CurIndex = 0;
var photoGallery2Count = 0;
function prepPhotoGallery2() {
	photoGallery2Count = $('#photoGallery2 div.image').length;
	$('#photoGallery2 .meta .count').empty().append( photoGallery2Count );
	
	$('#photoGallery2 a.close').click(function(){
		/*// reset to first item
		photoGallery2CurIndex = 0;
		$('#photoGallery2 .lining').scrollTo( $('#photoGallery2 .inner div.image:eq(0)') , {duration:0, axis:"x"});
		$('#photoGallery2 .meta .current').empty().append( '1' );
		*/
		$('#photoGallery2').fadeOut('fast', function(){
    		$('#screenImages').fadeIn('fast');
    	});
	});
	
	$('#photoGallery2 a.next').click(function(){
		if (photoGallery2CurIndex == photoGallery2Count-1) {
			photoGallery2CurIndex = 0;
		} else {
			photoGallery2CurIndex++;
		}
		
		$('#photoGallery2 .lining').scrollTo( $('#photoGallery2 .inner div.image:eq('+photoGallery2CurIndex+')') , {duration:700, axis:"x"});
		$('#photoGallery2 .meta .current').empty().append( photoGallery2CurIndex+1 );
	});
	$('#photoGallery2 a.previous').click(function(){
		if (photoGallery2CurIndex == 0) {
			photoGallery2CurIndex = photoGallery2Count-1;
		} else {
			photoGallery2CurIndex--;
		}
		
		$('#photoGallery2 .lining').scrollTo( $('#photoGallery2 .inner div.image:eq('+photoGallery2CurIndex+')') , {duration:700, axis:"x"});
		$('#photoGallery2 .meta .current').empty().append( photoGallery2CurIndex+1 );
	});
}

$(function() {
    
    // Tooltips
    $('#language a.lang').tooltip({
        showURL: false,
        track: false,
        extraClass: 'language'
    });
    
    $('a.hotspot').tooltip({
        track: false,
        showURL: false,
        bodyHandler: function() {
            var tooltipText = $(this).html();
            // debug -- var tooltipText = $(this).attr('id').replace('hs-','');
            return '<div class="top"></div><div class="bottom">'+ tooltipText +'</div>';
        },
        extraClass: 'hotspot',
        top: 1,
        left: 1
    }).hover(function() {
        $(this).addClass('hotspot-hover');
    }, function() {
        $(this).removeClass('hotspot-hover');
    });

    
    $('a.explore').click(function(){
    	$(window).scrollTo('a.exploreAnchor', {duration:700});
    	return false;
    });
    
    $('a.toTop').click(function(){
    	$(window).scrollTo(0, {duration:700});
    	return false;
    });
    
    $('#detailColRight div.views a').click(function(){
    	var rel = $(this).attr('rel');
    	$('#detailColRight div.views a').removeClass('active');
    	$(this).addClass('active');
    	
    	$('#photoGallery, #photoGallery2').fadeOut('fast', function(){
    		$('#screenImages').fadeIn('fast', function(){
    			$('#wrap #detail #detailColMiddle .view:visible').fadeOut('fast', function(){
    	    		$('#wrap #detail #detailColMiddle .view'+rel).fadeIn('fast');
    	    	});
    		});
    	});
    	
    	return false;
    });
    
    $('#detailColRight a.screen').click(function(){
    	$('#screenImages').fadeOut('fast', function(){
    		$('#photoGallery2').fadeIn('fast');
    		photoGallery2CurIndex = 0;
    		$('#photoGallery2 .lining').scrollTo( $('#photoGallery2 .inner div.image:eq(0)') , {duration:0, axis:"x"});
    		$('#photoGallery2 .meta .current').empty().append( '1' );
    	});
    	return false;
    });
    
    $('#detailColRight a.gallery').click(function(){
    	$('#screenImages').fadeOut('fast', function(){
    		$('#photoGallery').fadeIn('fast');
    		photoGalleryCurIndex = 0;
    		$('#photoGallery .lining').scrollTo( $('#photoGallery .inner div.image:eq(0)') , {duration:0, axis:"x"});
    		$('#photoGallery .meta .current').empty().append( '1' );
    	});
    	return false;
    });
    
    
    var regFormOn = false;
    $('#header a.register').click(function(){
    	if (regFormOn) {
    		regFormOn = false;
    		$('#register-form').slideUp();
    	} else {
    		regFormOn = true;
    		$('#register-form').slideDown();
    	}
    	return false;
    });
    
    $('#register-form .form-text input').focus(function(){
    	if (this.value == this.defaultValue) {
    		this.value = '';
    	}
    }).blur(function(){
    	if (this.value == '') {
    		this.value = this.defaultValue;
    	}
    });
    
    $('#register-form a.close').click(function(){
    	$('#header a.register').click();
    	return false;
    });
    
    
    $('button').hover(
    	function(){ $(this).addClass('hover'); },
    	function(){ $(this).removeClass('hover'); }
    );
    
    
    $('#register-form').submit(function(){
    	var valid = true;
    	
        if(valid) {
            $.post(
                $('#register-form').attr('action'),
                $('#register-form').serialize() + '&async=1',
                function(data, textStatus) {
                	if (data.status == 'ok') {
                        $('#register-form .innerLining *').css({"visibility":"hidden"});
                        $('#register-form .innerLining').append("<p class='thanks'>"+data.message+"</p>");
                        
                        regFormOn = false;
                		window.setTimeout("$('#register-form').slideUp();", 3000);
                	} else {
                		$('#register-form .innerLining p.error').remove();
                		$('#register-form .innerLining').prepend('<p class="error">'+data.message+'</p>');
                		
                	}
                },
                "json"
            );
        }
        
    	return false;
    });
    
    $('#newsRoomColRight .image').each(function(i, el){
    	$(el).addClass('col_'+ (i%3 +1) );
    });
    
    if ($.browser.safari) {
    	$('body').addClass('safari');
    }
    if ($.browser.mozilla) {
    	$('body').addClass('moz');
    }
    
    $(window).resize(geoSizePage);
    
    prepPhotoGallery();
    geoSizePage();
    
    // debug
    //$('#header a.register').click();
    //
    
    $('#header #register-form').css('top', (57 + $('#header a.register').height())+'px' );
    // alert( $('#header a.register').height() ); - 30
    
    var wh = $(window).height();
    if ($('body').height() < wh) {
    	$('body').css('height', wh+'px');
    }
    
});
