/*
	Author: Iankulov Miodrag
*/

$whitestarbooking = {
	init : function() {
		$whitestarbooking._news();
		$whitestarbooking._css();
		$whitestarbooking._table();
		$whitestarbooking._gallery();
		$whitestarbooking._gallery_pag();
		$whitestarbooking._videos();
		$whitestarbooking.booking();
		$whitestarbooking.carusel();
		$whitestarbooking.v_aImageLoad();
		$whitestarbooking.show_artists();
	},
	/*
		Show artists
	*/
	show_artists : function() {
		$('.show_artist').live('click',function() {
			$_rel = $(this).attr('rel');
			$('#slider').empty().load('/artist/show/'+$_rel);
			return false;
		});	
	},
	/*
		News effects
	*/
	_news : function() {
		$("#news dd:not(:first)").hide();
		$("#news dt a").click(function(){
			$("#news dd:visible").slideUp("slow");
			$(this).parent().next().slideDown("slow");
			return false;
		});	
                

               
  	},
	/*
		Css
	*/
	_css : function() {
		$('#news dd p:last-child').css('marginBottom','0px');
	},
	/*
		Index table effect
	*/
	_table : function() {
		$('.list_events tr:not(:first) td').hover(function(){
			$(this).parent().find('td').css('backgroundColor','#f2f1ec');
		},function(){
			$(this).parent().find('td').css('backgroundColor','white');
		});		
	},
	/*
		Gallery table bottom
	*/
	_gallery : function() {
		$('.v_gallery li:first').addClass('s').find('a').click();		
		
		/*
			When someone clicks on a thumb
		*/
		$('.v_gallery a').live('click',function(){
			var _____url = $(this).attr('href');
			/*
				Put the image
			*/
			$('#slider').addClass('align_center').html('<img src="'+_____url+'" alt="photo" class="top" style="height:400px"/>').find('img').hide().fadeIn(4000);
			/*
				For the pagination
			*/
			
			/*
				Remove the old selected
			*/
			$('.v_gallery li').removeClass('s');
			/*
				Adds to the current element
			*/
			$(this).parent().addClass('s');
			return false;
		});
	},
	/*
		Pagination
	*/	
	_gallery_pag : function() {
		$('#top_pag a').live('click',function() {
			var _type = $(this).attr('rel');
			
			switch(_type)
			{
				case '1':
					if($('.v_gallery li:last').hasClass('s'))
					{
						$('.v_gallery li:first').find('a').click();
					}else if($('.v_gallery li:first').hasClass('s')){
						alert('You can\'t go back!You are at the first page!');
					}else if(!$('.v_gallery li:first').hasClass('s') || $('.v_gallery li:last').hasClass('s')){
						$('.v_gallery li.s').prev().find('a').click();
					}
				break;
				case'2':
					if($('.v_gallery li:last').hasClass('s'))
					{
						$('.v_gallery li:first').find('a').click();
					}else{
						$('.v_gallery li.s').next().find('a').click();
					}					
				break;
			}
			return false;
		});		
	},
	/*
		Videos page
	*/
	_videos : function() {
		$('#videos ul li').hover(function(){
			$(this).find('span').slideDown();
		},function() {
			$(this).find('span').slideUp();
		});
	$('#news_videos ul li').hover(function(){
			$(this).find('span').slideDown();
		},function() {
			$(this).find('span').slideUp();
		});	
	
	},
	/*
		Booking
	*/
	booking : function() {

                  $('ul#nav li a').live('click',function(){
			var _name = $(this).attr('rel');
			 $.ajax({
			   type: "POST",
			   url: "/artist/changeLanguage",
                           data: "Language="+_name,
                           //data: "Id="+_id,
			   success: function(msg){
			    $('#biography').html(msg);
			    //$('body').animate({scrollTop:0}, 'slow');
			   }
			 });
			return false;
	     	   });
 
                  $('#news_videos ul.v li a').live('click',function(){
			var _name = $(this).attr('rel');
			 $.ajax({
			   type: "POST",
			   url: "/video/getvideo",
                           data: "name="+_name,
			   success: function(msg){
			    $('#slider').html(msg);
			    $('body').animate({scrollTop:0}, 'slow');
			   }
			 });
			return false;
		});

		$('#bookingForm').submit(function() { 
		    $(this).ajaxSubmit(function(data){
		    	$('#giveme').html(data);
		    	$('html, body').animate({scrollTop:0}, 'slow');

		    }); 
		    return false; 
		}); 
		
 
		$('#videos ul.v li a').live('click',function(){
			var _id = $(this).attr('rel');
			 $.ajax({
			   type: "POST",
			   url: "/video/getvideo",
			   data: "id="+_id,
			   success: function(msg){
			    $('#slider').html(msg);
			    $('body').animate({scrollTop:0}, 'slow');
			   }
			 });
			return false;
		});


                	
	},
	/*
		View artist image load
	*/
	v_aImageLoad : function() {
		$('.caruselWrap ul li a').live('click',function() {
			$_t    = $(this);
			$_href = $_t.attr('href');
			$_slider = $('#slider');
			$_slider.addClass('align_center').empty();
			
			var img = new Image();
		    $(img).load(function () {
	            //$(this).css('display', 'none'); // .hide() doesn't work in Safari when the element isn't on the DOM already
	            $(this).hide();
	            $(this).css('display','inline');
	            $_slider.prepend(this);
	            
	            $(this).fadeIn(2000);
	            
		    }).error(function () {
				alert('No logo image loaded!');
	            // notify the user that the image could not be loaded
		    }).attr('src', $_href).css({'display':'inline','marginTop':'20px'});
		    return false;
	    });	
	},
	/*
		View artist carusel
	*/
	carusel : function() {
		incrementWith = 910;
		wrapUL   = $('.caruselWrap ul');
		firstLi  = $('.caruselWrap li:first');
		//_size    = $('.caruselWrap ul li').length * (parseInt(firstLi.outerWidth(), 10) + parseInt(firstLi.css('marginRight'), 10) + parseInt(firstLi.css('marginLeft'), 10));
		_theSize = 130 * $('.caruselWrap ul li').length;
		
		
		wrapUL.width(_theSize);
		function Next()
		{
			if(wrapUL.is(':animated') || (-(_size-incrementWith) >= parseInt(wrapUL.css('left'), 10)) ){
				return;
			}
			wrapUL.animate({
				left:'-='+incrementWith
			});	
		}
		
		function Back() {
			if(wrapUL.is(':animated') || parseInt(wrapUL.css('left'), 10) ===0 ){return;}
			wrapUL.animate({
				left:'+='+incrementWith
			});
		};

		$('#scrollNext').click(function(){
			Next();
			return false;
		});
		
		$('#scrollBack').click(function(){
			Back();
			return false;
		});
	}
};

$(document).ready(function(){
	$('<div id="ajaxBusy">Loading...</div>')
        .ajaxStart(function() {$(this).show();})
        .ajaxStop(function() {$(this).hide();})
        .appendTo('body');
	$whitestarbooking.init()
	/*
		Rounded corners IE
		FUCK BILL GATES AND IE!!!!
	*/
});

