$(document).ready(function(){
	mainpage_basics();
	myship_basics();
	setInterval(refreshClock, 60000);
	scroll();
	$('#feedback').css('top', $(window).height()/2-50+'px');
	$("#select_rank_cancel").live("click", function(){
		$("#modal_window").dialog("close"); 
	});
}); 

function myship_basics(){
    $('.text_input').hint().focus(function(){
    	$(this).addClass('text_input_clicked');
    });
    
    $('.text_input').blur(function(){
    	$(this).removeClass('text_input_clicked');
    });
    
    
    $('#footer-right_select > a:eq(0)').hover(function(){
    	$(this).find('img').attr('src', '/images/clock-arrow-up-hilighted-ship.gif');
    }, function(){
    	$(this).find('img').attr('src', '/images/clock-arrow-up-ship.gif');
    }).click(function(){
    	$.getJSON($('#footer-right_select > input:eq(0)').val(), function(data){
    		$('#footer-datetime > span:eq(0)').text(data.time);
    		$('#footer-datetime > span:eq(1)').text(data.date);
    	});
    });
    
    $('#footer-right_select > a:eq(1)').hover(function(){
    	$(this).find('img').attr('src', '/images/clock-arrow-down-hilighted-ship.gif');
    }, function(){
    	$(this).find('img').attr('src', '/images/clock-arrow-down-ship.gif');
    }).click(function(){
    	$.getJSON($('#footer-right_select > input:eq(1)').val(), function(data){
    		$('#footer-datetime > span:eq(0)').text(data.time);
    		$('#footer-datetime > span:eq(1)').text(data.date);
    	});
    });

    $('.searchpos').myCorners({ident: 'cacaca', top: true});
    $('#searchfield').myCorners({ident: 'search', all: true});
    $('.flash-message').myCorners({ident: 'cacaca-yellow', all: true, extension: '.png'});    

    $('#send_proposition').live("click", function(){
		if($(this).val() == $(this).attr('alt'))
		{
			$(this).val('');
			$(this).css('color', '#2c2c2c');
		}
	});
	
	$("#send_proposition").live('keydown', function(){
		$(this).click();
	});
	
	$("#send_proposition_button").live('click', function(){
		var href = "mailto:myship@myship.com?subject=RankProposition&body="+$("#send_proposition").val();
		parent.location.href = href;
	});

    if($.browser.msie)
    {
		if(parseInt(jQuery.browser.version) <7)
		{
		    $('.invitation-image > a > img').each(function(){
		    	if($(this).width() > $(this).height())
		    	{
		    		var ratio = $(this).height() / $(this).width();
		    		$(this).css('width', '200px');
		    		$(this).css('height', ratio*200+'px');
		    	}
		    	else
		    	{
		    		var ratio = $(this).width() / $(this).height();
		    		$(this).css('height', '200px');
		    		$(this).css('width', ratio*200+'px');
		    	}
		    });
		}
    }
    
    $('input').filter(function(index){
	    	return  $(this).attr('type') == 'button' || $(this).attr('type') == 'submit';
	    }).css('cursor', 'pointer');
    $('a').css('cursor', 'pointer');
    $('textarea').autoResize();
    $(document).pngFix();
    if($('#prelogin').size() == 0)
    	$('#container').css('min-height', clientHeight()-$('#footer').height()-40+'px');
    
    $('#main_stats').dialog({
    	autoOpen: false,
    	modal: true,
    	width: 800,
    	height: 500,
    	dialogClass: "main_stats_dialog",
	title: "Crew<i>Index</i>"
    });
    
    $('#main_stats_btn').click(function(){
    	$('#main_stats').dialog('open');
    });
    
    $('#passrec_dialog').dialog({
    	autoOpen: false,
    	modal: true,
    	width: 800,
    	height: 500,
    	dialogClass: "pass_rec_dialog",
    	resizable: false,
    	title: $('#passrec_title').val()
    });
    
    $('#pass_forgot').click(function(){
    	$('#passrec_dialog').dialog('open');
    	return false;
    });
    
    $('#passrec_form').submit(function(){
    	var valid = true;
    	if($("#passrec_newpass").length > 0)
    	{
	    	$("#passrec_newpass").keyup();
			$("#passrec_retypepass").keyup();
			
			var valid = ($("#passrec_newpass").val().length >= 6 && $("#passrec_retypepass").val() == $("#passrec_newpass").val());
			if( ! valid)
				$('#change-pass').prev().show();
    	}

    	if(valid)
    	{
	    	$.post(
	    		$(this).attr('action'),
	    		$(this).serialize(),
	    		function(data){
	    			$('#passrec_dialog').html($(data).html());
	    		}
	    	);
    	}
    	return false;
    });
    
    if($('#passrec_open').length && $('#passrec_open').val() == "1")
    	$('#passrec_dialog').dialog("open");
    
    $('#new-comment textarea').focus(function(){
		if($(this).val() == $(this).attr('hint'))
		{
			$(this).val('');
			$(this).siblings('p').show();
			$(this).css('color', '#000000');
		}
	});	
	$('#new-comment textarea').blur(function(){
		if($(this).val().length == 0)
		{
			$(this).val($(this).attr('hint'));
			$(this).siblings('p').hide();
			$(this).css('color', '#0d7fbd');
			$(this).height(25);
		}
	});	
    
    $('#new-comment input[type=button]').click(function(){
		var f = $(this).parents('form:first');
		f.append($('#wall_type').clone());
		var d = $(f).serialize();
		f.find('#wall_type').remove();
		$.post(
			$(f).attr('action'),
			$(f).serialize(),
			function(data){
				$('.comment:first').css('margin-top', '1px').children('img').hide();
				if($('.comment:first').length)
					$('.comment:first').before($(data));
				else
					$('#new-comment').before($(data));
				var h = $(data).height();
				$(data).height(0).css('display', 'block').animate({height: h+"px"}, 100, function(){$(data).css('height', 'auto');});
				$('html, body').animate({scrollTop:$('#comments').offset().top}, 300);
			}
		);
		
		f.find('textarea').val('');
		
		return false;
	});
    
    $('.delete-profile-comment').live('click', function(){
    	var link = $(this);
    	$('#delete-comment-question').msgBox('open',{
    		okCallback: function(){
    			$.get(
			   		$(link).attr('href'),
			   		function(){
			   			var parent = $(link).parents('.comment:first');
		   				$(parent).remove();
		   				$('.comment:first').css('margin-top', '13px').children('img').show();
			   		}
    			);
    		}
    	});
    	
    	return false;
    });
    
    $('#delete-comment-question').msgBox('create', {
    	title: $('#delete-comment-question > input[name=title]').val()
    });
}


function clientHeight() {
    var height = 0;
    if( typeof( window.innerWidth ) == 'number' ) {
	    	//Non-IE
	    	height = window.innerHeight;
    	}
    	else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    		//IE 6+ in 'standards compliant mode'
    		height = document.documentElement.clientHeight;
    	}
    	else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    		//IE 4 compatible
    		height = document.body.clientHeight;
    	} 
    return height;
}

function refreshClock()
{
	$.getJSON($('#footer-right_select > input:eq(2)').val(), function(data){
		$('#footer-datetime > span:eq(0)').text(data.time);
		$('#footer-datetime > span:eq(1)').text(data.date);
	});
}

function mainpage_basics()
{
	var images_amount = 5;
	var hilighted_image = Math.ceil(images_amount/2)-1;
	scroller_left = 0;
	
	update_active_photo(hilighted_image);
	scroll();
	
	$("#scroller-left").click(function(){
		clearTimeout(scroller_timer);
		scroller_timer = setTimeout(scroll, scroller_timeout);
		$("#scroller").stop(true, true);
		update_last_photo(hilighted_image);
		var elem = $(".slider-sailors:last").clone();
		$(".slider-sailors:first").before(elem);
		$(".slider-sailors:last").remove();
		$('#scroller').css("left", "-190px");
		$('#scroller').animate({left : 0}, {complete: function(){
			update_active_photo(hilighted_image);
		}, duration:"slow"});
	});
	
	$("#scroller-right").click(function(){
		clearTimeout(scroller_timer);
		scroller_timer = setTimeout(scroll, scroller_timeout);
		$("#scroller").stop(true, true);
		update_last_photo(hilighted_image);
		var elem = $(".slider-sailors:first").clone();
		$(".slider-sailors:last").after(elem);
		$('#scroller').animate({left : -190}, {complete: function(){
			$(".slider-sailors:first").remove();
			$('#scroller').css("left", "0px");
			update_active_photo(hilighted_image);
		}, duration:"slow"});
	});
}

function update_active_photo(image_no)
{
	var elem = $(".slider-sailors:eq("+image_no+")");
	$(elem).find('.slider-sailor-photo').animate({'width':'100px','height':'100px'});
	$(elem).find('.slider-sailor-photo').find('img').animate({'width':'100px','height':'100px'});
	$(elem).find('.slider-sailor-background').animate({'width':'112px','height':'112px'});
	$(elem).find('.slider-sailor-background').find('img').animate({'width':'112px','height':'112px'});
	$(elem).find('.slider-sailor-photo').css('right', '14px');
	$('#scroller-cloud').show();
	$('.scroller-cloud-middle').text($(elem).find('input').val());
}

function update_last_photo(image_no)
{
	var elem = $(".slider-sailors:eq("+image_no+")");
	$(elem).find('.slider-sailor-background').stop(true, true);
	$(elem).find('.slider-sailor-background').find('img').stop(true, true);
	$(elem).find('.slider-sailor-photo').stop(true, true);
	$(elem).find('.slider-sailor-photo').find('img').stop(true, true);
	$(elem).find('.slider-sailor-background').css('width', '57px').css('height', '57px');
	$(elem).find('.slider-sailor-background').find('img').css('width', '57px').css('height', '57px');
	$(elem).find('.slider-sailor-photo').css('width', '50px').css('height', '50px');
	$(elem).find('.slider-sailor-photo').find('img').css('width', '50px').css('height', '50px');
	$(elem).find('.slider-sailor-photo').css('right', '10px');
	$('#scroller-cloud').hide();
}


var scroller_timer = 0;
var scroller_timeout = 3000;

function scroll()
{
	$("#scroller-left").trigger('click');
	clearTimeout(scroller_timer);
	scroller_timer = setTimeout(scroll, scroller_timeout);
}
