//Author: Werner de Bruin
//File: Common JS site functions

//Cycle through aticles
	var currA =1;
	function rotateArticles(s){							
		var numArticles =$('.'+s+'A').size();
		$('.'+s+'P > div').html("<img src='ui/images/pgs.off.png' width='12' height='23' />");
		$('#'+s+'A' + currA).hide();					
		if(s=='mp') { $('#'+s+'Footer > div:visible').hide(); }
		currA =(currA >=numArticles-1) ? 0 : currA + 1;
		
		if(s=='lg')currA=0; //Hack - Laughing gear for some reason defaults to second without this
		$('#'+s+'A' + currA).fadeIn(2500);
		if(s=='mp') { $('.'+s+'Footer' + currA).fadeIn(2500); }
		$('#'+s+'P' + currA +' > div').html("<img src='ui/images/pgs.on.png' width='12' height='23' />");
	}   
	
	//Build article pagination
	function articlePgs(s) {//s=section
		var numArticles =$('.'+s+'A').size();		
		for(var c=0;c<numArticles;c++){			
			if(c==0) {
				$('#'+s+'Pgs').append("<div id='"+s+"P"+c+"' class='"+s+"P'><div><img src='ui/images/pgs.on.png' width='12' height='23' /></div></div>");
			} else {
				$('#'+s+'Pgs').append("<div id='"+s+"P"+c+"' class='"+s+"P'><div><img src='ui/images/pgs.off.png' width='12' height='23' /></div></div>");
			}		
		}
	}
	
	//Paging
	function pageThrough(s,currA) {//s=section, currA=current article
		if(s=='mp') { pauseArticles(); }
		$('.'+s+'Copy:visible').hide();		
		$('.'+s+'A').hide();
		if(s=='mp') { $('#'+s+'Footer > div:visible').hide(); }
		$('.'+s+'P > div').html("<img src='ui/images/pgs.off.png' width='12' height='23' />");
		$('#'+s+'A' + currA).fadeIn(2500);
		if(s=='mp') { $('.'+s+'Footer' + currA).fadeIn(2500); }
		$('#'+s+'P' + currA +' > div').html("<img src='ui/images/pgs.on.png' width='12' height='23' />");	
	}
	
	function compTCsLightbox(aID){//aID = WP article/post ID
		$('.jqmOverlay').fadeIn();				
		$('.jqmWindow').fadeIn(500);		
		$.ajax({
			type: 'POST',
			url: '/publish/callPost.php?aID='+aID+'&cachebuster=' + 1*new Date(),
			success: function(content){					
				$('.jqmWindow div#content').html(content);
				return false;
			}	
		});	
	}
	
	function lightbox(aID){	//aID = WP article/post ID
		$('.jqmOverlay').fadeIn();				
		$('.jqmWindow').fadeIn(500);
		if(aID=='fbinvitefriends.php') {
			$.ajax({
				type: 'POST',
				url: 'fbinvitefriends.php?cachebuster=' + 1*new Date(),
				success: function(content){					
					$('.jqmWindow div#content').html(content);
					return false;
				}	
			});	
		} else if(aID=='feedback.php'){
			$.ajax({
				type: 'POST',
				url: 'feedback.php?cachebuster=' + 1*new Date(),
				success: function(content){			
					$('.jqmWindow div#content').html('<iframe src="feedback.php" frameborder="0" scrolling="no" width="440" height="287"></iframe>');
					return false;
				}	
			});	
		} else if(aID=='storytip.php'){
			$.ajax({
				type: 'POST',
				url: 'storytip.php?cachebuster=' + 1*new Date(),
				success: function(content){			
					$('.jqmWindow div#content').html('<iframe src="storytip.php" frameborder="0" scrolling="no" width="440" height="287"></iframe>');
					return false;
				}	
			});	
		} else if(aID=='privacy.php'){
			$.ajax({
				type: 'POST',
				url: 'privacy.php?cachebuster=' + 1*new Date(),
				success: function(content){			
					$('.jqmWindow div#content').html(content);
					return false;
				}	
			});	
		} else if(aID=='about'){
			$.ajax({
				type: 'POST',
				url: '/publish/callPost.php?aID=60&cachebuster=' + 1*new Date(),
				success: function(content){					
					$('.jqmWindow div#content').html(content);
					return false;
				}	
			});	
		} else if(aID.indexOf('YTvid')==-1){
			$.ajax({
				type: 'POST',
				url: '/publish/callPost.php?aID='+aID+'&cachebuster=' + 1*new Date(),
				success: function(content){					
					$('.jqmWindow div#content').html(content);
					return false;
				}	
			});	
		} else {
			var src =aID.substring(6,aID.length);
			var YT ='<br /><object width="460" height="287" id="myytplayer"><param name="movie" value="http://www.youtube.com/v/'+src+'&hl=en_GB&fs=1&"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed id="myytplayer" src="http://www.youtube.com/v/'+src+'&hl=en_GB&fs=1&" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="460" height="287"></embed></object><br /><br />';
			$('.jqmWindow div#content').html(YT);			
		}	
		scroll(0,0);
	}
	
	function bookmark(url,title){ //bookmark HSA
		if ((navigator.appName == "Microsoft Internet Explorer") && (parseInt(navigator.appVersion) >= 4)) {
			window.external.AddFavorite(url,title);
		} else if (navigator.appName == "Netscape") {
			window.sidebar.addPanel(title,url,"");
		} else if (navigator.appName == "Netscape") {
			alert("Press CTRL-D (Safari) or CTRL-T (Opera) to bookmark");
		}     
    }

	//SHARE BUTTONS USE ACROSS
	var shareDiscussImg ='<img src="ui/images/icon.shareDiscuss.png" width="23" height="23" border="0" alt="discuss" align="left" /><div class="padLft5 padTop5">DISCUSS</div>';
	var shareFacebookImg ='<img src="ui/images/icon.shareFacebook.png" width="23" height="23" border="0" alt="post to wall" align="left" /><div class="padLft5 padTop5">POST TO WALL</div>';
	var shareTwitterImg ='<img src="ui/images/icon.shareTwitter.png" width="23" height="23" border="0" alt="tweet this" align="left" /><div class="padLft5 padTop5">TWEET THIS</div>';	
	var shareSendtipImg ='<img src="ui/images/icon.shareSendtip.png" width="23" height="23" border="0" alt="send story tip" align="left" /><div class="padLft5 padTop5">SEND A STORY TIP</div>';
	
	//DISCUSS IN GASBAG
	function discuss(topic){
		document.location.href ='/gasbag/discuss.php?tt='+topic;
	}
	
	$(document).ready(function(){		

		//FB Connect init
		FB.init('14d90b48f55a1505d7d817ce172d4e59', 'xd_receiver.htm', {"ifUserConnected" : update_user_box});
			
		//SHARE LINK ROLLOVERS
		$("li[class^='share']").live('mouseover',function() {//rollover		
			var onImg ='ui/images/icon.'+$(this).attr('class')+'.ro.png';
			$(this).css({'color':'#555'});//,'background-image':onImg
		});
		$("li[class^='share']").live('mouseout',function() {//rollout
			var offImg ='ui/images/icon.'+$(this).attr('class')+'.png';
			$(this).css({'color':'#999'});//,'background-image':offImg
		});
		
		//BUTTON ROLLOVERS
		$("*[class^='btn']").live('mouseover',function() {//rollover
			var srcImg =$(this).attr('class');
			var onImg ='ui/images/btn.'+srcImg.substr(3,srcImg.length)+'.ro.png';			
			$(this).attr('src',onImg);
		});
		$("*[class^='btn']").live('mouseout',function() {//rollout
			var srcImg =$(this).attr('class');			
			var offImg ='ui/images/btn.'+srcImg.substr(3,srcImg.length)+'.png';
			$(this).attr('src',offImg);
		});
		
		//CLOSE LIGHTBOX
		$('.jqmWindow .closeBtn').live('click', function() {
			$('.jqmWindow').fadeOut();
			$('.jqmOverlay').fadeOut(500);	
			//IE 6 - stop YouTube player			
			/*if(ytplayer) {
				$("#myytplayer").stopVideo();				
			}*/						
		});		
		
	});	