
	/*
	#
	#	homepage.js
	#	home page functions
	#	author : moshi delgo
	#	company : Rotev,inc.
	#
	*/

	//CONF
	var DESCRIPTION_MAX_LENGTH = 30;
	var USERNAME_MAX_LENGTH = 13;
	var COMMENTS_COUNT = 4;
	var COMMENT_TEXT_MAX_LENGTH = 95;
	var COMMENTS_TIMER_REFRESH = 30000;  
	var VIRALLITY_TOKEN = '';

	var virallity_api_version = "v1.0";

	var interval_comments_refresh = null;
	
	var comments_ids = [];

	$(document).ready(function(){

		// call public feeds
		home.recent_feed(0,9);
		home.popular_feed('today',0,5);
		//home.commentsPublicFeed(0,COMMENTS_COUNT);
		//bind more info button
		$("#more_info_switch").bind('click',function(){
			$('#more_info').slideToggle('slow',function (){
				if ( $("#more_info").is(':visible') ) {
  					$("#more_info_switch").text("less...");
				}else{
					$("#more_info_switch").text("more...");
				}
			});
			return false;
		});
		$("#next_time").bind('click',function(){home.dont_show_moreinfo();return false;});

		//set timers
		home.set_intervals();

	});


var home = {
	 /**
         * Utility function to make ajax calls.  Assumes json response and relative urls.
         **/
        ajax_call : function(url, callback, callback_error) {

          jQuery.ajax({
                        type: "GET",
                        url: url,
                        dataType:"json",
                        success: callback,
                        error: function(xhr, statusText, error) {
                                if(xhr.status != 500 || statusText == "timeout") { //attempt to prevent weird feedback loop
                                        home.error_message("Opps sorry, an error has occurred. Please logout and try again");
                                }
                        }
                });

        }, //ajaxCall

	set_intervals : function (){
		
		interval_comments_refresh = setInterval(home.update_commentsPublicFeed,COMMENTS_TIMER_REFRESH); //set comments refresh timer.

	},//set_intervals

        dont_show_moreinfo : function (){
                var expiredays = 365;
		var domain = "virallity.com";

                var exdate=new Date();
                exdate.setDate(exdate.getDate()+expiredays);
                document.cookie= "more_info" + "=dont_show" + ((expiredays==null) ? "" : ";expires="+exdate.toGMTString()) + ( ( domain ) ? ";domain=" + domain : "" );
	
		var next_time = document.getElementById("next_time");
		next_time.setAttribute("style","color:#6C737A");
		$(next_time).text('Will not be shown next time');

        },//set_VIRALLITY_FEED_TYPE

        decode_html_entities : function(s) {
                return s.replace(/&apos;/g, "'").replace(/&quot;/g, '"').replace('<p>','');
        },

	getToken : function (token){
		if (VIRALLITY_TOKEN == '')
				VIRALLITY_TOKEN = token;

		return VIRALLITY_TOKEN;
	},

        /*
        #       get most shared videos feed.
        */

	popular_feed : function (time,start,count){

		$("#loading_popular_shared_videos").show();
		var ajax_url = "/api/"+virallity_api_version+"/services.php?service=publicFeed&time="+time+"&start="+start+"&count="+count;
                home.ajax_call(ajax_url,function (json){home.popular_feed_callback(json,time,start,count)});

	},//popular_feed

	popular_feed_callback : function (videos,time,start,count){
	
		var container = document.getElementById("contain_popular_share");
                while(container.hasChildNodes()){
                        container.removeChild(container.firstChild);
                }

		var catagory = new Array('Today','Week','Month','Year');	
	
		var ul = document.createElement("ul");
	
		for (i=0;i<catagory.length;i++){
			var li = document.createElement("li");
			if (time == catagory[i].toLowerCase()){
				li.setAttribute((document.all ? 'className' : 'class'), "selected_cat");
			}
			var li_link = document.createElement("a");
			li_link.setAttribute("href","#");
			$(li_link).bind('click',{cat:catagory[i].toLowerCase()},function(event){home.popular_feed(event.data.cat,0,5);return false;});
			$(li_link).text(catagory[i]);
			li.appendChild(li_link);
			ul.appendChild(li);
		}

		var rss_feed = document.createElement("li");
		rss_feed.setAttribute((document.all ? 'className' : 'class'), "rss_feed_li");
		var rss_link = document.createElement("a");
		rss_link.setAttribute("href","/public_feed_rss.php?time="+time+"&start="+start+"&count=10");
		rss_feed.appendChild(rss_link);
		var rss_img = document.createElement("img");
		rss_img.setAttribute("src","/images/rss-feed-icon.gif");
		rss_img.setAttribute((document.all ? 'className' : 'class'), "rss_feed_img");	
		rss_link.appendChild(rss_img);
		ul.appendChild(rss_feed);		
	
		container.appendChild(ul);
			
	
		if (videos.length >  0){
                	for (i=0;i<videos.length;i++){
                	        var video = home.buildRecentVideo(videos[i]);
                	        container.appendChild(video);
                	}
			
			var nav = document.createElement("div");
			nav.setAttribute("id","navigate_popular_videos");
			
			var nav_ul = document.createElement("ul");
			nav.appendChild(nav_ul);
			
			if (start > 0 ){
				var nav_li_prev = document.createElement("li");
				nav_ul.appendChild(nav_li_prev);
		   		var prev = document.createElement("a");
		                prev.setAttribute("href","#");
				$(prev).bind('click',{start:start,count:count,time:time},function (event){
					home.popular_feed(event.data.time,event.data.start-event.data.count,event.data.count);
					return false;
				});
	           	     	$(prev).text("prev");
	                	nav_li_prev.appendChild(prev);
			}

                        var nav_li_next = document.createElement("li");
                        nav_ul.appendChild(nav_li_next);
                        var next = document.createElement("a");
                        next.setAttribute("href","#");
			$(next).bind('click',{start:start,count:count,time:time},function (event){
				home.popular_feed(event.data.time,event.data.start+event.data.count,event.data.count);
				return false;
			});
                        $(next).text("next");
                        nav_li_next.appendChild(next);

			container.appendChild(nav);


		}else{
			if (time == 'today'){
				var txt_time = 'Today';
			}else{
				var txt_time = 'this '+time;
			}
			var txt_div = document.createElement("div");
			txt_div.setAttribute("style","margin-left:20px;");
			var txt = document.createTextNode("No videos shared "+txt_time+" yet , try different catagory.");
			txt_div.appendChild(txt);
			container.appendChild(txt_div);
		}

		$("#loading_popular_shared_videos").hide();

	},//popular_feed_callback

	/*
	#	get recent videos feed.
	*/
	recent_feed : function (start,count){
		
		var ajax_url = "/api/"+virallity_api_version+"/services.php?service=publicFeed&time=recent&start="+start+"&count="+count;
		home.ajax_call(ajax_url,function (json){home.recent_feed_callback(json)});
	},//public_feed

	/*
	#	public feed callback
	*/
	recent_feed_callback : function (videos){

		var container = document.getElementById("contain_recent_shared");
   		while(container.hasChildNodes()){
	       		container.removeChild(container.firstChild);
		}
	
		for (i=0;i<videos.length;i++){
			var video = home.buildRecentVideo(videos[i]);
			container.appendChild(video);
		}

	},//public_feed_callback
	
	buildRecentVideo : function (video){
		
		if (video.video_url_videosurf != "null" && video.video_url_videosurf != "none")
			var url = video.video_url_videosurf;
		else
			var url = video.video_url;
		
		var video_contain = document.createElement("div");
		video_contain.setAttribute((document.all ? 'className' : 'class'), "recent_video");

		var img_link = document.createElement("a");
		img_link.setAttribute("href",url);
		img_link.setAttribute("id","img_bg");
		var img = document.createElement("img");
		img.setAttribute("src",video.video_thumbnail);
		img_link.appendChild(img);
		video_contain.appendChild(img_link);

		var title = document.createElement("a");
		title.setAttribute("href",url);
		title.setAttribute((document.all ? 'className' : 'class'), "recent_title");
		$(title).text(video.video_title);
		video_contain.appendChild(title);		

		var description = document.createElement("p");
		description.setAttribute((document.all ? 'className' : 'class'), "recent_description");
		var Desc = home.decode_html_entities(video.video_description).replace(/\n/g, "");
		if(Desc.length > DESCRIPTION_MAX_LENGTH) {
			Desc = Desc.substr(0,DESCRIPTION_MAX_LENGTH)+"...";
		}

		$(description).text(Desc);
		video_contain.appendChild(description);

		var share_by = document.createElement("div");
		//share_by.setAttribute("class","shared_by");
		share_by.setAttribute((document.all ? 'className' : 'class'), "recent_shared_by");
		if (video.video_source != undefined && video.user_name != null){
                
			//var share_by_text = document.createTextNode("Shared by "+videos[I].user_name+" on "+videos[I].video_source);
			//var share_by_text = document.createTextNode("Shared ");
			//share_by.appendChild(share_by_text);
                
			if (video.user_id > 0 ){
				var share_by_text = document.createTextNode("Shared by ");
				share_by.appendChild(share_by_text);

				var share_by_user = document.createElement("a");
				share_by_user.setAttribute("href","/posted.php?id="+video.user_id);
				var username = video.user_name;
				if (username.length > USERNAME_MAX_LENGTH){
					username = username.substr(0,USERNAME_MAX_LENGTH)+"...";
				}
				$(share_by_user).text(video.user_name);
				share_by.appendChild(share_by_user);
			}else{
				/*var share_by_user = document.createElement("span");
				share_by_user.setAttribute((document.all ? 'className' : 'class'), "share_by_user_span");
				if (video.user_id == 'Private')
					share_by_user.setAttribute("style","color:red");
                                var username = video.user_id;
                                if (username.length > USERNAME_MAX_LENGTH){
                                        username = username.substr(0,USERNAME_MAX_LENGTH)+"...";
                                }
				var share_by_user_txt = document.createTextNode(username);
				share_by_user.appendChild(share_by_user_txt);
				share_by.appendChild(share_by_user);*/
				
				var share_by_text = document.createTextNode("Shared on ");
                                share_by.appendChild(share_by_text);
		
				var share_by_user = document.createElement("span");
				share_by_user.setAttribute((document.all ? 'className' : 'class'), "share_by_user_span");
				var share_by_user_txt = document.createTextNode(video.video_source);
				share_by_user.appendChild(share_by_user_txt);
				share_by.appendChild(share_by_user);
			} 

		}

		video_contain.appendChild(share_by);

		var dis_time = document.createElement("p");
		dis_time.setAttribute((document.all ? 'className' : 'class'), "recent_display_time");
		$(dis_time).text(video.date_added_display);
		video_contain.appendChild(dis_time);

		var report = document.createElement("a");
		report.setAttribute("href","#");
		report.setAttribute((document.all ? 'className' : 'class'), "report_video");
		var report_txt = document.createTextNode("Report Video");
                report.appendChild(report_txt);
		//var report_img = document.createElement("img");
		//report_img.setAttribute("src","/images/report.png");
		//report_img.setAttribute("style","width:4px;height:9px;border: medium none; float: left;margin: 2px;");
		//report.appendChild(report_img);
		//$(report).text("report");
		//var report_txt = document.createTextNode("report");
		//report.appendChild(report_txt);
		//$(report).bind("click",{id:video.video_id,video:video_contain},function (e){home.report_video(e.data.id,e.data.video);return false;});
		$(report).bind("click",{id:video.video_id,button:report},function (e){home.report_video(e.data.id,e.data.button);return false;});
		video_contain.appendChild(report);

		return video_contain;
	},

        commentsPublicFeed : function (start,count){

                var ajax_url = "/api/services.php?service=publicCommFeed&start="+start+"&count="+count;
                home.ajax_call(ajax_url,function (json){home.commentsPublicFeed_callback(json,start,count)});

        },//popular_feed

	update_commentsPublicFeed : function(){

                var ajax_url = "/api/services.php?service=publicCommFeed&start=0&count="+COMMENTS_COUNT;
                home.ajax_call(ajax_url,function (json){home.commentsPublicFeed_callback(json,0,COMMENTS_COUNT)});


	},//update_commentsPublicFeed

	commentsPublicFeed_callback : function (comments,start,count){

		var container = document.getElementById("recent_made_comments_container");
                for (i=0;i<comments.length;i++){
			if (comments_ids[comments[i].id] != true){
				
				comments_ids[comments[i].id] = true;
                        	var comment = home.buildcomment(comments[i]);
                        	//container.appendChild(comment);
				if (container.firstChild)
					container.insertBefore(comment,container.firstChild);
				else
					container.appendChild(comment);

				$(comment).show('slow');
			}
                }

	},//commentsPublicFeed_callback


	buildcomment : function (comment){
		var contain_comment = document.createElement("div");
		contain_comment.setAttribute((document.all ? 'className' : 'class'), "comment");
		
		var img = document.createElement("img");
		img.setAttribute("src",comment.pic);
		contain_comment.appendChild(img);

		if (comment.user_id>0){		
			var link = document.createElement("a");
			link.setAttribute((document.all ? 'className' : 'class'), "username");
			link.setAttribute("href","http://www.virallity.com/posted.php?id="+comment.user_id);
			$(link).text(comment.name);
			contain_comment.appendChild(link);
		}else{
			var name = document.createElement("span");
			name.setAttribute("style","font-weight:bold");
			$(name).text(comment.user_id);
			contain_comment.appendChild(name);
		}

                var comment_text = home.decode_html_entities(comment.comment_text).replace(/\n/g, "");
                if(comment_text.length > COMMENT_TEXT_MAX_LENGTH) {
                        comment_text = comment_text.substr(0,COMMENT_TEXT_MAX_LENGTH)+"...";
                }

		var text = document.createElement("span");
		$(text).text(comment_text);
		contain_comment.appendChild(text);

                if (comment.url_videosurf != "null" && comment.url_videosurf != "none")
                        var url = comment.url_videosurf;
                else
                        var url = comment.url;

		var link_video = document.createElement("a");
		link_video.setAttribute("href",url);
		$(link_video).text(comment.title);
		contain_comment.appendChild(link_video);

		return contain_comment;
	},//buildcomment

	 report_video : function (id,button){
		var token = home.getToken();
			
		var ajax_url = "/api/services.php?service=report_video&id="+id;
		if (token)
			ajax_url= ajax_url + "&token="+ token;
                home.ajax_call(ajax_url,function (json){$(button).text('Thanks');});
        }


}
