$(document).ready(function(){ 
	
	thecalendarFunctions();
	cheatsFunctions();
	
	$('#datebox').click(function(){ 
		$('#thecalendar').toggle();
	});
	
	$('#monthselect').change( function() { 								   
		DailybeastAjax.changeMobileCalendar( $('#monthselect option:selected').attr('rel') )	;	
	});
	
	  
});
 

function thecalendarFunctions(){
	
	
	$('#thecalendar .dates .date').click(function(){
		 
		newMonth = $(this).parent('.dates').attr('rel');
		newDay = $(this).html();
		DailybeastAjax.changeMobileCalendarDate( newMonth , newDay );
		$('#thecalendar .dates .current').removeClass('current');
		
		monthString = $(this).parent('.dates').attr('rev');
		yearString = $(this).parent('.dates').attr('title');
		
		$('#datebox span').html( monthString+' '+newDay+', '+yearString );
		
		$(this).addClass('current');
		
		document.selectedMonth = newMonth;
		document.selectedDay = newDay;
		$('#thecalendar').hide();
		$('#cheats_holder').html('<div class="loading_message">Loading<br /><img src="http://www.tdbimg.com/image/mobile_loadingbar.gif" /></div>');
	 	
		
	});
	
	
	$('#thecalendar .clickzone').click(function(){ $('#monthselect').focus(); })
	
}

function cheatsFunctions(){
	
	$('.articlerow').each( function(i){
		setClickOpen( this );
	});
	
}

function setHead(p_sType){
	$('#headlogo').attr('src','http://www.tdbimg.com/image/mobile_head_'+p_sType+'.gif');
}


function setClickOpen( element ){
	
	$(element).click(function(){ 
		$('#thecalendar').hide();
		var tmpElement = this;
		$(this).unbind();
		$(this).find('.arrowdown').hide();
		$(this).children('.text').slideDown('normal', function(){
			setClickClose( element );
		});
		
	});
}

function setClickClose( element ){
	
	$(element).find('.icon').show();
	
	var titleClose = $(element).find('.titleblock');
	var arrowClose = $(element).find('.arrowup');
	
	actualClose(titleClose);
	actualClose(arrowClose);
	
	
	function actualClose( clickableElement ){
		
		$(clickableElement).click(function(){
			$(element).find('.icon').hide();
			$('#thecalendar').hide();
			$(element).find('.arrowdown').show();
			$(titleClose).unbind();
			$(arrowClose).unbind();
			
			$(element).children('.text').slideUp('normal', function(){
				setClickOpen ( $(element) );
			});
		});
	};
	
	
}



showGalleryMobile = function(galleryId){
	
	DailybeastAjax.getGalleryForMobile(galleryId);
	
}



showLightboxForGallery = function(l_nIndex){
	
	l_aItems = $('#galleryinfo_holder .galleryitem');
	
	l_nLength = l_aItems.length;
	
	//mobile_error_dummy.gif
	
	//Make goofy lightbox object:
	
	//l_xObject = {};
	
	
	//l_xObject.href = (  $(l_aItems[l_nIndex]).attr('rel')  );
	

	showLightbox(l_aItems[l_nIndex],  true);
	
	
	showLightboxForGallery_callback = function(){
		
		if( $(l_aItems[l_nIndex]).children('.caption').html() ){
			
			$('#lightboxCaption').append(
				'<div class="clear"></div>'+
				$(l_aItems[l_nIndex]).html()
			);
		}
		
		$('#lightboxCaption').append('<div class="clear"></div>');
		
		if( (l_nIndex) !=  0 ){
			$('#lightboxCaption').append('<a onclick="javascript:closeLightboxOpenGallery('+(l_nIndex-1)+');" href="javascript:void(0);">GoLeft</a>');
		}
		
		if( (l_nIndex+1) < l_nLength  ){
			$('#lightboxCaption').append('<a onclick="javascript:closeLightboxOpenGallery('+(l_nIndex+1)+');" href="javascript:void(0);">GoRight</a>');
		}
		
	}
	
}

		
closeLightboxOpenGallery = function(p_nIndex){
	
	l_aItems = $('#galleryinfo_holder .galleryitem');
	l_nLength = l_aItems.length;
	
	hideLightbox();
	
	$('#ajaxhelper').animate({ opacity: .9999}, 0100, '', function() { 
		showLightboxForGallery(p_nIndex,  true );
	});
	
}



/* ---------------------- Ajax Functions -------------------------------------- */

DailybeastAjax.changeMobileCalendar = function(p_nMonthChange) { 
    DailybeastAjax._xArgs.data = 'a=changeMobileCalendar';
    DailybeastAjax._xArgs.data += '&p_nMonthChange='+p_nMonthChange;
    DailybeastAjax._xArgs.data += '&p_nSelectedMonth='+document.selectedMonth;
    DailybeastAjax._xArgs.data += '&p_nSelectedDay='+document.selectedDay;
	DailybeastAjax._xArgs.returnDiv = '#thecalendar';
	DailybeastAjax._xArgs.callBack = DailybeastAjax.changeMobileCalendar_callback;
    DailybeastAjax.makeAjaxCall('/include/ajax.php');
}


DailybeastAjax.changeMobileCalendar_callback = function(){
	thecalendarFunctions();
}


DailybeastAjax.changeMobileCalendarDate = function(p_nNewMonth,p_nNewDay){
	
    DailybeastAjax._xArgs.data = 'a=changeMobileCalendarDate';
    DailybeastAjax._xArgs.data += '&p_nNewMonth='+p_nNewMonth;
    DailybeastAjax._xArgs.data += '&p_nNewDay='+p_nNewDay;
	DailybeastAjax._xArgs.returnDiv = '#cheats_holder';
	DailybeastAjax._xArgs.callBack = DailybeastAjax.changeMobileCalendarDate_callback;
    DailybeastAjax.makeAjaxCall('/include/ajax.php');
}

DailybeastAjax.changeMobileCalendarDate_callback = function(){
	cheatsFunctions();
	$('#thecalendar').hide();
	lightboxImageSearch();
}




DailybeastAjax.getGalleryForMobile = function(p_nGalleryId){
    DailybeastAjax._xArgs.data = 'a=getGalleryForMobile';
    DailybeastAjax._xArgs.data += '&p_nGalleryId='+p_nGalleryId;
	DailybeastAjax._xArgs.returnDiv = '#galleryinfo_holder';
	DailybeastAjax._xArgs.callBack = DailybeastAjax.getGalleryForMobile_callback;
    DailybeastAjax.makeAjaxCall('/include/ajax.php');
}

DailybeastAjax.getGalleryForMobile_callback = function(){
	
	showLightboxForGallery(0);
	
	
}
