var galleryUrl = '/templates/gallery_display.php';
var cheatAssetUrl = '/templates/cheat_asset_display.php';



fixAssetOverlay = function(newW, newH){
	oldWidth = parseInt( $('#TB_iframeContent').width() );
	
	moveLeft = (oldWidth - newW) / 2;
	
	$('#TB_iframeContent').height(newH);
	$('#TB_iframeContent').width(newW);
	
	$('#TB_iframeContent').css('left',moveLeft );
	
	$('#TB_iframeContent').css('visibility','visible');
}


showCheatImage = function(assetUri){
	tb_show('',cheatAssetUrl+'?TB_iframe&assetType=1&assetUri='+assetUri+'&width=646&height=628');	
	$('#TB_iframeContent').css('visibility','hidden');
	
	setTimeout(function(){	
		tmpOffset =  ( $(document).scrollTop() );
		$('#TB_overlay').height(  $('#TB_overlay').height(    )+tmpOffset  );
		
		
	}, 100);

}


showCheatVideo = function(assetUri, assetName){
	tb_show('',cheatAssetUrl+'?TB_iframe&assetType=2&assetUri='+assetUri+'&assetName=&width=305&height=347');
	
	setTimeout(function(){	
		tmpOffset =  ( $(document).scrollTop() );
		$('#TB_overlay').height(  $('#TB_overlay').height(    )+tmpOffset  );
		
	}, 100);
}


showGallery = function(galleryId ,galleryIndex , noResetHash){
	
		
		if(galleryIndex == null || galleryIndex == 'undefined')
			galleryIndex = 0;
		
		 
		
		tb_show('',galleryUrl+'?TB_iframe&galleryId='+galleryId+'&galleryIndex='+galleryIndex+'&width=646&height=628');
		
		DailybeastAjax.getMainGalleryInfo (galleryId);
		
	
		setTimeout(function(){	
			tmpOffset =  ( $(document).scrollTop() );
			$('#TB_overlay').height(  $('#TB_overlay').height(    )+tmpOffset  );
			
			
		}, 100);
}





tb_remove = function(){
	tmpOffset =  ( $(document).scrollTop() );
	parent.location.hash = '#';
	$.scrollTo(tmpOffset,0);
	tb_remove_true();
}


DailybeastAjax.getMainGalleryInfo = function(p_nGalleryId) {
    DailybeastAjax._xArgs.data = 'a=getMainGalleryInfo';
    DailybeastAjax._xArgs.data += '&p_nGalleryId='+p_nGalleryId;
	
 
	DailybeastAjax._xArgs.callBack = DailybeastAjax.getMainGalleryInfo_callback;
    DailybeastAjax.makeAjaxCall('/include/ajax.php');
	
}



DailybeastAjax.getMainGalleryInfo_callback = function(){
	
	var  returnObj = DailybeastAjax.returnVar.split('~!~');
	
	if(returnObj[0]){
		$('#TB_window').append('<div id="gallery_bg"><img src="'+returnObj[0]+'"></div>');
	}
	if(returnObj[1] && returnObj[2]){
		alert(DailybeastAjax.returnVar);
		$('#TB_window').append('<div id="gallery_ad"><div class="gallery_top"></div><div class="gallery_header_overlay"> <div class="gap10"></div><img src="http://www.tdbimg.com/image/d20title_'+returnObj[2]+'.png" alt="Gallery" /><div class="gap10"></div><div style="width:468px;height:70px;margin:0 auto;">'+returnObj[1]+'</div></div></div>');
	}
	
}

$(document).ready(function(){ 
	
	if(parent.location.hash){
		
		var l_aHash = parent.location.hash.split(';');
		
		indexOfGalleryId = 	l_aHash[0].search(/gallery=/);
		
		if(indexOfGalleryId >= 1)
			galleryId =  l_aHash[0].slice(indexOfGalleryId+8) ;
		else
			return
		
		indexOfPage = 	l_aHash[1].search(/page=/);
		
		
		if(indexOfPage === 0)
			galleryIndex =  l_aHash[1].slice(indexOfPage+5) -1 ;
		else
			galleryIndex = 0;
		
		showGallery(galleryId, galleryIndex, 1);
		
		
	}
	
});
