/* 
 * The scripts within this file only works if JQuery javascript file is included in the page befor this file.
 */

/**
 * Remove text value if texts == textToRemove 
 * where textToRemove is the default text such as search here...
 * @param texts textfeild value 
 * @param textToRemove textfeild value (default value)
 */
function removeText(texts, textToRemove){
	jQuery(document).ready(function($) { 
		var text = $(texts).val();
	    if(text == textToRemove){
	        $(texts).val("");
	        $(texts).removeClass();
	        $(texts).addClass('fieldDark');
	    }	
	});
}

/**
 * Remove text value if texts == textToRemove 
 * where textToRemove is the default text such as search here...
 * @param texts textfeild value 
 * @param textToRemove textfeild value (default value)
 * @param cssClass	-CSS class 
 */
function removeText1(texts, textToRemove, cssClass){
	jQuery(document).ready(function($) { 
		var text = $(texts).val();
	    if(text == textToRemove){
	        $(texts).val("");
	        $(texts).removeClass();
	        $(texts).addClass(cssClass);
	    }	
	});
}


function viewInfo(tip) {
	if (!document.getElementById('tooltip'))
		newelement('tooltip');
	var lixlpixel_tooltip = document.getElementById('tooltip');
	lixlpixel_tooltip.innerHTML = tip;
	lixlpixel_tooltip.style.display = 'block';
	document.onmousemove = getmouseposition;
}
function exit() {
	document.getElementById('tooltip').style.display = 'none';
}

function openclose(id) {
	jQuery(document).ready(function($) {
		if ($('#blogDetails' + id).css('display') == 'none') {
			$('#blogDetails' + id).show('slow');
			
			// header: hide plus sign
			$('#header' + id).children('#open').hide(0);					
			// header: show minus sign
			$('#header' + id).children('#close').show(0);

		} else {
			// hide the contents
			$('#blogDetails' + id).hide('slow');
			
			// header: show plus sign
			$('#header' + id).children('#open').show(0);

			// header: hide minus sign
			$('#header' + id).children('#close').hide(0);
		}
	});
}

function showOrHide(id,showhide) {
	jQuery(document).ready(function($) {
		if (showhide == true) {
			$(id).show('slow');

		} else {
			// hide the contents
			$(id).hide('slow');
		}
	});
}

function quickShow(div) {
	jQuery(document).ready(function($) {
		if ($('#blogDetails' + div).css('display') == 'none') {
			$('#blogDetails' + div).css('display', 'block');
			$('#openclose' + div).attr('src', close);

		}
	});
}

function FaceJar(){
    //var m_data = 5;
    //this.ShowText = DisplayText; 
	
	/**do not change this*/
	var facejarURL = "http://www.facejar.com/";
	var logged = "No";
	/**
	 * Check if this member is logged on.
	 * @param page
	 */
	this.isLoggedMember = function(){
	    var serverurl = facejarURL+"user/multi-used-pages/request_handler.jsp?user=default&action=IS MEMBER LOGGED";
	    //var logged = "No";
  		var facejar = new FaceJar();
		var logged1 = "No";
		//jQuery(document).ready(function ($) {
		jQuery.ajax({		
		        url: serverurl,
		        async: false, 		               
		        //Do not cache the page 
		        cache: false,			            
		        dataType: "html",
				success: function(html){
					logged1 = jQuery.trim(html); 
				    //return logged1;  
				},
				//Error handling
				error: function (xhr, status, error){
				    logged1 = error; 
			    }   
			});  
		//}); 
		
	    return logged1;    
	};
	
	/** trackdown the difference between previous watch and new watched */
	var previousWatchID = 0;
	/** Show realtime update of channels watched.*/
	this.showChannelsWatched = function(){ 
		var serverurl = facejarURL+"section/app/request_handler.php?realtimeupdate=yes";
		jQuery(document).ready(function ($) {
    		//$.noConflict();
		    $.post(serverurl, {
		    	queryString : ""
		     },function(data) {
		      	if (data.length > 0) {
		      		var faded = false;
		      		// this is set in request_handler.php page for new channel
		    		if($("#checknew").length > 0){
		    			var newWatchID = $("#checknew").text();
		    			// flash the realtimeupdate div to alert user about recent watched
		            	if(previousWatchID != newWatchID){
		            		$(".realtimeupdate").hide().html(data).fadeIn("slow");
		              		//$('.realtimeupdate').effect("highlight", {}, 2000);
			            	faded = true;
		            	}
		            	previousWatchID = newWatchID;
		    		} 
		    		
		    		// only do this at first time loading 
		    		if(!faded){
			      		$('.realtimeupdate').html(data); 		    			
		    		}			
		      	}
		    });	     
	    });

  		// In order for setTimeout function to recognize showChannelsWatched function
  		// we need to create a new instance. 
  		var facejar = new FaceJar();
		window.setTimeout(facejar.showChannelsWatched, 20000);	     
    };
	
	
	/**
	 * Like or dislike a topic.
	 */
	this.likedislike = function(formid,like){      			
  			var topicIDRequest = formid;
  			var TopicOpinion = like;
  			var data = "topicIDRequest="+topicIDRequest+
  					   "&action=OPINION&actionType=SUBTOPIC&TopicOpinion="+TopicOpinion;
  			
  			jQuery(document).ready(function ($) {
	  			//alert(1);
	  			$('#opinionwait'+formid).fadeIn('slow').html('<font color="blue">Please wait...</font>');
	  		    $.ajax({		
						url: facejarURL+"servlet/TopicServlet", 
				        type: "GET", 			  
				        data: data,		           
				        cache: false,			            
				        dataType: "xml",
						success: function(xml){									
							var errorMsg = $(xml).find("ErrorMessage").text();
							if(errorMsg != null && errorMsg.length > 0){
				      			$('#opinionwait'+formid).html('<font color="white">'+errorMsg+'</font>');
								
							}else{
				      			$('#opinionwait'+formid).fadeIn('slow').html('<font color="green">Done!</font>');
				      			var description = "";
				      			var opinion = "";
				      			$('#youropinion'+formid).fadeIn('slow').append('<font color="blue">You '+TopicOpinion+'!</font>');			      			
							}
						} ,
						//Error handling
						error: function (xhr, status, error){	
			      			$('#opinionwait'+formid).html('<font color="red">Error occurred '+error+'</font>').fadeIn('slow');
					    }      			
	 	        }); 
  			});
    }
	
	/**
	 * Check if this member is logged on then write header.
	 */
	this.writeLoggedOnHeader = function(){
		var response = this.isLoggedMember();
		jQuery(document).ready(function ($) {
			if(response == "YES"){		
			    var serverurl = facejarURL+"user/multi-used-pages/headerLogged.jsp";
			    var logged = "No";
			    $.ajax({		
			        url: serverurl,
			        async: false, 		               
			        //Do not cache the page 
			        cache: false,			            
			        dataType: "html",
					success: function(html){
						$("#header1").html(html);	
					},
					//Error handling
					error: function (xhr, status, error){
						logged = error;
				    }   
				}); 				
			} 
		}); 	  
	};

	/**
	 * Check if this member is logged on then write footer.
	 */
	this.writeLoggedOnFooter = function(){
		var response = this.isLoggedMember(); 
		jQuery(document).ready(function ($) {
			if(response == "YES"){		
			    var serverurl = facejarURL+"user/multi-used-pages/footerLogged.jsp";
			    var logged = "No";
			    $.ajax({		
			        url: serverurl,
			        async: false, 		               
			        //Do not cache the page 
			        cache: false,			            
			        dataType: "html",
					success: function(html){
						$("#footer-container").html(html);	
					},
					//Error handling
					error: function (xhr, status, error){
						logged = error;
				    }   
				}); 				
			} 
		});   
	};
	
	/**
	 * Show realtime update of activities.
	 */
	this.activitiesShow = function(){
  		var facejar1 = new FaceJar();
		jQuery(document).ready(function ($) {
			var response = facejar1.isLoggedMember(); 
			if(response == "YES"){		
			    var serverurl = facejarURL+"user/multi-used-pages/activities_alert.jsp?action=Activities Alert";
			    var logged = "No";
			    $.ajax({		
			        url: serverurl,
			        async: false, 		               
			        //Do not cache the page 
			        cache: false,			            
			        dataType: "html",
					success: function(html){
						$("#friends_appending_area").html(html);	
					},
					//Error handling
					error: function (xhr, status, error){
						logged = error;
				    }   
				}); 				
			}
		}); 		
		// keep updating after 20 seconds
		window.setTimeout(facejar1.activitiesShow, 20000);			
	};
	
	/**
	 * Show object can be html of user topics, photos or friends.
	 * This depends on parameters (params). the html will be viewed in div
	 */
	this.showObject = function(params,div){
		jQuery(document).ready(function ($) {
			$(div).html("Loading...");	
		    var serverurl = facejarURL+"user/multi-used-pages/view_object.jsp?"+params;
			$.ajax({		
				url: serverurl,
				async: false, 		               
				//Do not cache the page 
				cache: true,			            
				dataType: "html",
				success: function(html){
					$(div).html(html);	
				},
				//Error handling
				error: function (xhr, status, error){
					$(div).html(error);
				}   
			}); 
		}); 	  
	};
	
	/**
	 * trackdown user page.
	 */
	this.trackPage = function(){
		var pathname = window.location.href.replace(/&/g,"-");
	    var serverurl = facejarURL+"section/app/request_handler.php?user=default&pagetracker="+pathname;
		jQuery(document).ready(function ($) {
			$.ajax({		
				url: serverurl,
				async: false, 		               
				//Do not cache the page 
				cache: false,			            
				dataType: "html",
				success: function(html){},
				//Error handling
				error: function (xhr, status, error){}   
			}); 
		});
	};
	
	/**
	 * Show the bottom ad area as well as all of it's contents.
	 */
	this.writeBottomAds = function(){
		jQuery(document).ready(function ($) {
		    var serverurl = facejarURL+"section/include/bottom_ads.php";
			$.ajax({		
				url: serverurl,
				async: false, 		               
				//Do not cache the page 
				cache: true,			            
				dataType: "html",
				success: function(html){
					$("#show_bottom_ads").html(html);	
				},
				//Error handling
				error: function (xhr, status, error){
					$("#show_bottom_ads").html(error);
				}   
			}); 
		}); 	  
	};
	
	/**
	 * Show html of countries list.
	 */
	this.writeCountries = function(){
		jQuery(document).ready(function ($) {
		    var serverurl = facejarURL+"section/app/request_handler.php?countries=ALL";
			$.ajax({		
				url: serverurl,
				async: false, 		      
				cache: true,			            
				dataType: "html",
				success: function(html){
					$("#show_countries").html(html);	
				},
				//Error handling
				error: function (xhr, status, error){
					$("#show_countries").html(error);
				}   
			}); 
		}); 	  
	};

	
	/**
	 * Show html of popular TV Channels.
	 */
	this.writePopularChannels = function(){
		jQuery(document).ready(function ($) {
		    var serverurl = facejarURL+"section/app/request_handler.php?Channels=MostWatched";
			$.ajax({		
				url: serverurl,
				async: false, 		      
				cache: true,			            
				dataType: "html",
				success: function(html){
					$("#popular_channels").html(html);	
				},
				//Error handling
				error: function (xhr, status, error){
					$("#popular_channels").html(error);
				}   
			}); 
		}); 	  
	};
	
}
//End FaceJar class

/**Adjust the table size with window size
$(document).ready(function() {
    $('#windowTable').height($(window).height());
    
});
*/

