//$('.hoverrating').hover( rateCallbackMouseOver, rateCallbackMouseOut );
//$('body').bind('click',function(e){closeRating(e);});
function rateCallbackMouseOver(e){
	$('div.dyna').each(function(){
									$(this).remove();
								});

	var rootNode = $(this).attr('uplevel') == '1' ? $(this).parent() : $(this).parent().parent();
	var refid    = $(this).attr('refid');
	if ( !rootNode.children('div.dyna').length ){
		var div    = ce('div');
		var left   = $(this).attr('offsetLeft');
		var top    = $(this).attr('offsetTop');
		var width  = $(this).attr('offsetWidth');
		var height  = $(this).attr('offsetHeight');
		$(div).css({'position':'absolute','top':(height-top)+'px','left':left+2+'px','zIndex':'2000','color':'#000','border':'0px solid red','width':'80px','height':'20px','display':'inline','padding':'0px','lineHeight':'10px'});
		$(div).attr('className','dyna');
		$(div).addClass('nofur');
		$(div).html('<table cellpadding="0" cellspacing="0" class="nofur" onmouseout="closeRating(event);"><tr><td class="nofur rate_star_bar"><div id="1" class="nofur rate_star" onmouseover="toggleHighlightStar(this, event)" onclick="updateRateValue(this,'+refid+',event)"></div><div id="2" class="nofur rate_star" onmouseover="toggleHighlightStar(this, event)" onclick="updateRateValue(this,'+refid+',event)"></div><div id="3" class="nofur rate_star" onmouseover="toggleHighlightStar(this, event)" onclick="updateRateValue(this,'+refid+',event)"></div><div id="4" class="nofur rate_star" onmouseover="toggleHighlightStar(this, event,event)" onclick="updateRateValue(this,'+refid+',event)"></div><div id="5" class="nofur rate_star" onmouseover="toggleHighlightStar(this, event,event)" onclick="updateRateValue(this,'+refid+',event)"></div></td></tr></table>');
		rootNode.append(div);
		e.preventDefault();
		return false;		
	}
}
function rateCallbackMouseOut(e){
	var srcElement = e.target == null ? e.srcElement : e.target;
	var t = e.relatedTarget != null ? e.relatedTarget : e.toElement != null ? e.toElement : '';	
	var rootNode = $(this).attr('uplevel') == '1' ? $(this).parent() : $(this).parent().parent();
	if ( !$(t).hasClass('nofur') ) rootNode.children('div.dyna').remove();												
	e.preventDefault();
	return false;
}
function toggleHighlightStar( obj, e ){
	if( $(obj).hasClass('rate_star_select') ) {
		for(ds = 5; ds >= obj.id ; ds--){
			$('#'+ds).removeClass('rate_star_select')
			$('#'+ds).addClass('rate_star');		
		}
	} else {
		for(s = 1; s <= obj.id; s++) {
			$('#'+s).removeClass('rate_star')
			$('#'+s).addClass('rate_star_select');
		}
	}
}
function closeRating( e ){
	var t = e.relatedTarget != null ? e.relatedTarget : e.toElement != null ? e.toElement : '';
	if ( !$(t).hasClass('nofur') ) $('div.dyna').remove();	
}
function updateRateValue( obj, refid, e ){
	var ratevalue = $(obj).attr('id');
	//alert(refid);return;
	$.ajax({
				type : "POST",
				url	 : g_site_path+"ajax/rate-media",
				data : 'ratevalue='+ratevalue+'&media_id='+refid+'&rand='+Math.random(),
				dataType : 'html',
				success: function( data ) {
					closeRating(e);
					e.preventDefault();
					return false;
				}
		   });
}
function doViewImage( filter_type, media_id ) {
	
	d = new dialog( {width:770, height:450, onclose:'d.close()', ismodel:true, isdrag:false, isclose:false} );	
	d.sethtml('Loading...');
	d.show();
	
	$.ajax({
			type : "POST",
			url	 : g_site_path+"ajax/get-full-photo",
			data : 'filter_type='+filter_type+'&media_id='+media_id+'&rand='+Math.random(),
			dataType : 'html',
			success: function( data ) {
				d.sethtml( data );
			}
	   });	
}

function showPagingCaptionOver(e){
	
	var t = e.relatedTarget != null ? e.relatedTarget : e.toElement != null ? e.toElement : '';
	//$('#debugger').html($(this).parent().attr('offsetTop')+" "+e.pageY+" "+$(this).attr('offsetTop')+" "+$(this).attr('offsetHeight'));
	var img_height 	   = parseInt( $(this).height() );
	var correction_pos = parseInt( $(this).parent().attr('offsetTop') ) + parseInt( $(this).attr('offsetTop') );
	var tooltip_text = $(this).attr('alt'); 
	var range = 50;
	//$('#debugger').html('');
	//$('#debugger').html( (correction_pos+img_height-range).toString()+" "+(correction_pos+img_height).toString() );
	if ( e.pageY >= (correction_pos+img_height-range) && e.pageY <= (correction_pos+img_height) ){
		drawFloatingPanel( $(this), tooltip_text );		
		//$('#debugger').html('1');
	}else{
		$('#pagecaptiondiv').slideUp('slow',function(){$('.removethisdiv').remove();});
		//$('#debugger').html('2');
	}
	//}
	e.preventDefault();
	return false;	
}

var Browser = {
  	Version: function() {
   	var version = 999; // we assume a sane browser
    if (navigator.appVersion.indexOf("MSIE") != -1)
    	// bah, IE again, lets downgrade version number
     	version = parseFloat(navigator.appVersion.split("MSIE")[1]);
    	return version;
  	}
}

function drawFloatingPanel( obj, tooltip, pagingNeeded ){
	
	var pagingNeeded = !isdefined(pagingNeeded) ? 1 : pagingNeeded;
	var pnlTop = parseInt(obj.parent().attr('offsetHeight'))-57;
	var pnlArrowTop = pnlTop+20;
	var tooltip = "";
	if (Browser.Version() < 7) {
  		pnlArrowTop = 200;
	}
	$('#pagecaptiondiv').css({'top':pnlTop+'px','left':'0px','width':obj.parent().attr('offsetWidth')-2,'opacity':'0.6','borderTop':'5px solid #464646'});		
	$('#pagecaptiondiv').slideDown('slow',function(){
												   $('#PhotoContainer').find('div.remremrem').each(function(){
																											$(this).remove();
																											});//$('.remremrem').remove();
												   if ( pagingNeeded ){
													   $('#PhotoContainer').append(
																				   '<div class="remremrem"><div id="photopaging_prev" class="nofur removethisdiv" style="position:absolute;top:'+pnlArrowTop+'px;left:10px;line-height:0px;"><a href="Javascript:void(0);" onclick="moveprevimage()" title="Move Previous"><img src="'+g_template_img+'img_prev.gif" border="0" /></a></div><div align="center" style="top:'+pnlTop+'px;left:50px; width:690px;position:absolute;line-height:50px;font-weight:bold;color:#fff;" class="nofur removethisdiv">'+tooltip+'</div><div id="photopaging_next" style="position:absolute;top:'+pnlArrowTop+'px;left:'+parseInt(obj.parent().attr('offsetWidth')-22)+'px;line-height:0px;" class="nofur removethisdiv"><a href="Javascript:void(0);" onclick="movenextimage();" title="Move Next"><img src="'+g_template_img+'img_next.gif" border="0"  /></a></div></div>'
																				   );
														manageButtons();
												   }else{
												   		 $('#PhotoContainer').append(
																				   '<div class="remremrem"><div style="top:'+pnlTop+'px;left:'+parseInt(obj.parent().attr('offsetWidth'))/2+'px;position:absolute;line-height:50px;font-weight:bold;color:#fff;" class="nofur removethisdiv">'+tooltip+'</div></div>'
																				   );
												   }
													// $('#photopaging_prev').css('display','none');
													 //$('#photopaging_next').css('display','none');
												   });
}

function showPagingCaptionOut(e){
	var srcElement = e.target == null ? e.srcElement : e.target;
	var t = e.relatedTarget != null ? e.relatedTarget : e.toElement != null ? e.toElement : '';	
	//var rootNode = $(this).attr('uplevel') == '1' ? $(this).parent() : $(this).parent().parent();
	if ( !$(t).hasClass('nofur') ){		
		$('#pagecaptiondiv').slideUp('slow',function(){$('.removethisdiv').remove();});			
		//$('#debugger').html('3');
	}
	e.preventDefault();
	return false;	
}
function manageButtons(){
	$('.jcarousel-clip>ul li:first').each(function(){
												if ( $(this).children().find('a').hasClass('MPVideoHolderSelected') ){													
														$('#photopaging_prev').css('display','none');													
												}
											 });
	$('.jcarousel-clip>ul li:last').each(function(){	
											 	if ( $(this).children().find('a').hasClass('MPVideoHolderSelected') ){													
													if ( $('.jcarousel-next').attr('disabled') == "true" )
														$('#photopaging_next').css('display','none');													
												}
											 });
	//$('#debugger').html($('#photopaging_prev').css('display'))
}
function moveprevimage(){
	var media_id, img_path, img_title, index=0;
	$('.jcarousel-clip>ul li').each(function(){	
												var id = $(this).children().find('a').attr('id');
												if ( $(this).children().find('a').hasClass('MPVideoHolderSelected') ){																										
													//alert(index+" "+parseInt($('#hdnGalleryTotal').attr('value')));
													if( Math.abs(index - parseInt($('#hdnGalleryTotal').attr('value'))) % 7 == 0 ) $('.jcarousel-prev').trigger('click');
													//if( index % 7 == 1 && index > 1 ) $('.jcarousel-prev').trigger('click');													
													showOriginalImage(img_path, media_id, img_title);														
													return false;
												}
												media_id = id.substring(id.lastIndexOf("_")+1);																						
												$(this).children().find('img').each(function(){
																								img_path  = $(this).attr('src_med');
																								img_title = $(this).attr('alt');																								
																							});
												index++;
											 });
}

function movenextimage(){
	var show = 0, reloadCarousel=1, index=0;
	$('.jcarousel-clip>ul li').each(function(){											 	
												index++;
												if ( show == 1 ){
													var id = $(this).children().find('a').attr('id');
													var media_id = id.substring(id.lastIndexOf("_")+1);
													if( index % 7 == 1 && index > 1 ) $('.jcarousel-next').trigger('click');	
													$(this).children().find('img').each(function(){
																									showOriginalImage($(this).attr('src_med'), media_id, $(this).attr('alt'));																									
																									});
													reloadCarousel = 0;
													return false;
												}												
											 	if ( $(this).children().find('a').hasClass('MPVideoHolderSelected') ){													
													show = 1;
												}												
											 });
	if ( reloadCarousel == 1 ){
		$('.jcarousel-next').trigger('click');		
	}
}

$('body').bind('click',function(e){
									var t = e.relatedTarget != null ? e.relatedTarget : e.toElement != null ? e.toElement : '';
									if ( !$(t).hasClass('nofur') ){										
										$('#pagecaptiondiv').slideUp('slow',function(){$('.removethisdiv').remove();});
										//$('#debugger').html('4');
									}
								});


function showPhotoCaptionHover( e ){
	var pnlHeight = $(this).height();
	var pnlWidth  = $(this).width();
	var pnlLeft   = $(this).attr('offsetLeft');
	var pnlTop	  = $(this).attr('offsetTop');
	var rootNode  = $(this).attr('uplevel') == '1' ? $(this).parent() : $(this).parent().parent();
	$('.TempFloatPanel').remove();
	var floatRootDiv = ce('div');
	$(floatRootDiv).attr('className','TempFloatPanel');	
	var floatPnl  = ce('div');
	$(floatPnl).css({'position':'absolute','top':pnlTop,'left':pnlLeft, 'backgroundColor':'#000','opacity':'0.4','width':pnlWidth,'height':pnlHeight});
	$(floatRootDiv).append(floatPnl);
	$(rootNode).append(floatRootDiv);
	$(floatRootDiv).addClass('nofur');
	var textdiv = ce('div');
	$(textdiv).css({'position':'absolute','top':pnlTop+50,'left':pnlLeft, 'color':'#fff','opacity':'100','width':pnlWidth,'height':pnlHeight,'fontWeight':'bold','lineHeight':'10px'});
	$(textdiv).html( $(this).attr('title') );	
	$(floatRootDiv).append(textdiv);
	$(textdiv).addClass('nofur');
	
	$('.TempFloatPanel').bind('mouseout', hidePhotoCaptionOut);
	$('.TempFloatPanel').bind('click', function(e){$(this).prev().trigger('click');});
}

function hidePhotoCaptionOut( e ){
	var t = e.relatedTarget != null ? e.relatedTarget : e.toElement != null ? e.toElement : '';
	var src = e.target != null ? e.target : e.srcElement;
	if ( !$(src).hasClass('TempFloatPanel') ){
		if ( !$(t).hasClass('nofur') ){	
			$('.TempFloatPanel').remove();
		}
	}
}
function downloadMediaFromServer( path, mediaid ){
		
		if ( parseInt(mediaid) > 0 ){
			window.location.href = path+'?media_id='+mediaid;
		}
}

	$(document).ready( 
		function() { 
			$('#img_share').click( 
				function()  { 
					showEmbed()
				} 
			)
		}
	)
	
	function showEmbed( media_id ){
		//alert(media_id)
		var mid=$('#hdnDownloadMedia').val();
		
		if(!media_id){
				//alert(mid);
				$.post(g_site_path+"ajax/get-photo-share-screen", 
				   { mediaid:mid }, 
				   function(data){ 
						$('#share_button').css('display', 'none');
						$('#share_container_popup').html( data );
						//$('#share_button').after( data ); 
						setTimeout(function(){$('#share_button_over').attr('src',g_template_img+'button_share_over.gif');},500);
						/*$('#txt_embedcode').val( $('#video_player').html() ); */
				   } 
			);			
		}
		else{
			$.post(g_site_path+"ajax/get-photo-share-screen", 
				   { mediaid:media_id }, 
				   function(data){ 
						$('#share_button').css('display', 'none');
						$('#share_container_popup').html( data );
						//$('#share_button').after( data ); 
						setTimeout(function(){$('#share_button_over').attr('src',g_template_img+'button_share_over.gif');},500);
						/*$('#txt_embedcode').val( $('#video_player').html() ); */
				   } 
			);
		}
	}
	
	function closeEmbed() {
		$('#share_button').css('display', 'block');	
		$('#SharePopupContainers').remove();
	}	
	
	function sendVideoShareMail( media_id ) {
		var embed_code = trim( $('#txt_embedcode').val() );
		var emails = trim( $('#txt_email').val() );
		var your_email = trim( $('#txt_your_email').val() );
		var your_name  = trim( $('#txt_your_name').val() );
		var friend_name = trim( $('#txt_friend_name').val() );
		
		if( your_name == "" ) {
			$('#email_warnning').html( 'Please enter your name' );
			$('#txt_your_name').focus();
			return;
		}
		
		if( !isValidMultiEmail(your_email, 1000, ',') ) {
			$('#email_warnning').html( 'Please enter valid email' );
			$('#txt_your_email').focus();
			return;
		}
		if( friend_name == "" ) {
			$('#email_warnning').html( 'Please enter your friend name' );
			$('#txt_friend_name').focus();
			return;
		}
		if( !isValidMultiEmail(emails, 1000, ',') ) {
			$('#email_warnning').html( 'Please enter valid email' );
			$('#txt_email').focus();
			return;
		}
		$('#SharePopupContainers').html( '<span class="warning">Please wait...</span>' );
		$.post(g_site_path+"ajax/send-photo-embed-code", 
			   	{ mediaid:media_id,email:emails, 'your_name':your_name,'your_email':your_email, 'friend_name':friend_name }, 
				function(data){ 
				//alert(data);
					//$('#SharePopupContainers').html( data );
					$('#SharePopupContainers').html( '<span class="warning">Mail sent successfully</span>' );
					setTimeout(function(){closeEmbed() }, 1000);
				} 
		);
	}