// JavaScript Document

/***************************************
	VARIABLES
****************************************/
var video_currentStatus;
var video_currentTheme;
var video_currentVideo;
var video_currentSection;

//for Main view Page only
var video_currentPage;
var video_currentCommentsPage;
var video_NumPerPage;

//for Archive Page only
var video_currentYear;
var video_currentMonth;



//refers to the PHP File name
var thisPage;

/***************************************
	FUNCTIONS
****************************************/
	$(document).ready(function(){
		
		//----original code for intitializing Video page	 
		if(document.getElementById('year') != null){
				
					video_currentMonth='';
					video_currentCommentsPage=1;
					video_currentYear=document.getElementById('year').value;
					video_currentSection=document.getElementById('section').value;;
					video_currentPage=document.getElementById('pageNum').value;
					video_NumPerPage =document.getElementById('numPerPage').value;
					video_currentVideo =document.getElementById('video').value;
					video_currentTheme =document.getElementById('theme').value;
					thisPage = document.getElementById('currentPage').value;
					$('.vloading').hide();
					$.getScript('js/facebook.js'); 
					initialize_google_plus(); 
					
			}
			
				$('.videoPlayIcon').click(function(){
									$(window).scrollTop($("#navContainer_innerBox").offset().top);	   
				});
		//----original code for intitializing Video page
		
		
	
	});
    			

/*
	For music player communication
	
*/

//CORE.createModule("videoMusicHandle", function (sandbox) {
//	return {
//		intit: function(){
//			console.log('Initi Module');
//			 sandbox.listen({              
//                "child-update": this.infoUpdate,
//				
//            }); 
//			 sandbox.notify({
//				type: 'parent-pause'			   
//			});
//			 
//			 
//		
//
//
//			
//		},
//		destroy: function() {
//			
//		}
//	
//	}
//	
//});


/*	
 *	Sets current ITB STATUS variable
 */
function video_setCurrentSection(a){
	video_currentSection = a;				
}

/*	
 *	Sets current ITB STATUS variable
 */
function video_setCurrentStatus(a){
	video_currentStatus = a;				
}
/*	
 *	Sets current year variable
 */
function video_setCurrentTheme(a){
	video_currentTheme = a;				
}

/*	
 *	Sets current year variable
 */
function video_setCurrentVideo(a){
	video_currentVideo = a;				
}
/*	
 *	Sets current year variable
 */
function video_setCurrentYear(y){
	video_currentYear = y;				
}

/*	
 *	Sets current month variable
 */
function video_setCurrentMonth(m){
	video_currentMonth = m;				
}

/*	
 *	Sets current page variable
 */
function video_setCurrentPage(p){
	video_currentPage = p;				
}

/*	
 *	Sets current page variable
 */
function video_setCurrentCommentsPage(p){
	video_currentCommentsPage = p;				
}

/*	
 * Sets Year, and Month Variables, Fills news via ajax	
 */
function video_goToYM(y,m){
		showLoader(); 
	video_setCurrentYear(y);
	video_setCurrentMonth(m);	
	video_fill_archive('');
	
}

/*	
 * Sets page Fills video via ajax	
 */
function video_goToP(p){
		showLoader();
	video_setCurrentPage(p);
	video_fillVideos('');
}
/*	
 * Sets page, Fills Comments via ajax	
 */
function video_goToPComments(p){
		showLoader();
	video_setCurrentCommentsPage(p);
	video_fillComments();
}

/*	
 * Sets sets page to one, sets current theme, fills Theme index via ajax
 * calls all other Ajax sections
 */
function video_goToTheme(a){
		showLoader();
	video_setCurrentPage(1);
	video_setCurrentTheme(a);

	video_fillThemeIndex();
	
}

/*	
 * Sets Fills Comments via ajax	
 * resets page to one
 */
function video_showComments(a){
	showLoader();
	video_setCurrentCommentsPage(1);
	video_setCurrentVideo(a);
	video_goToPComments(1);
	
	video_fillComments();
}


function video_gotToArchive(){
	
		showLoader();
	video_goToYM(video_currentYear,video_currentMonth);
	
}

function goToVideo(thisTheme,vid,thisPage, section){
	
	$(window).scrollTop($("#videoBannerContainer").offset().top);

		showLoader();
	video_setCurrentSection(section);
	video_setCurrentTheme(thisTheme);
	video_setCurrentPage(thisPage);
	video_setCurrentVideo(vid);
	
	video_fillVideos('playVid');
}
function goToVideo_archive(thisTheme,vid,section){
	showLoader(); 
	video_setCurrentSection(section);
	video_setCurrentTheme(thisTheme);
	video_setCurrentPage(1);
	video_setCurrentVideo(vid);
	video_fill_archive('playVid');
}
function backToVideo(){
	
		showLoader();
	video_fillVideos('TOP');
	//$(window).scrollTop($("#navContainer_innerBox").offset().top);
}


function photo_Change_img_titles(a) {
	if(a == 1){
		$("#photo_content_img").attr("src","http://d1k2qty5tbyya1.cloudfront.net/images/assets/icons/title-videoList.png");  
   		  
	}else if(a == 2){
    	$("#photo_content_img").attr("src","http://d1k2qty5tbyya1.cloudfront.net/images/assets/icons/title-comments.png");
	}else{
    	 
		 $("#photo_content_img").attr("src","http://d1k2qty5tbyya1.cloudfront.net/images/assets/icons/title-videoList.png");
	}
}
/************************   AJAX   *****************************/


/*	Fills news page VIA ajax
 *	recieves NOTHING, uses preset varible
 */
function video_fillVideos(caller){
	photo_Change_img_titles(0);
        var data = 'theme='+video_currentTheme+'&video='+video_currentVideo+'&page='+video_currentPage+'&numPerPage='+video_NumPerPage+'&section='+video_currentSection;
        $.ajax({
            url: "include/video/video_Ajax.php",	
            type: "GET",		
            data: data,		
            cache: false,
            success: function (html) {	
			// html = html.substring(0, html.length-1);
		
			
                $('#Ajax_Container').html(html);
				
               		
					
				$('.vloading').hide();
				
				
				if(caller == 'playVid'){
					
					playVideo();
				}else if(caller == 'TOP'){
					hideLoader();
					$(window).scrollTop($("#navContainer_innerBox").offset().top);
				}else{
					hideLoader();
					$(window).scrollTop($("#photo_content_img").offset().top);
					
				}
				
				$('.videoPlayIcon').click(function(){
						$(window).scrollTop($("#navContainer_innerBox").offset().top);	   
				 });
            }		
        });
					
	
    }
				
function playVideo(){
	
	 document.getElementById("videoBox").innerHTML='<div id="altContentDiv_497897"></div><div class="nloading videoLoader"></div>';
	 document.getElementById("FB_BOX").innerHTML='';
	   var data = 'video='+video_currentVideo+'&section='+video_currentSection;
        $.ajax({
            url: "include/video/video_Banner_Ajax.php",	
            type: "GET",		
            data: data,		
            cache: false,
            success: function (data) {
			 var $response=$(data);
               //query the jq object for the values
			var thisVideo = $response.filter('#url').text();
				
			var themeTitle =  $('.themeTitleSelected_media strong').text();
			
			var videoName = $response.filter('#videoName').text();
			var videoDuration = $response.filter('#videoDuration').text();
			var videoComments = $response.filter('#videoComments').html();
			var FB = $response.filter('#FB').html();
			var videoDesc = $response.filter('#videoDesc').text();
			//alert(videoComments);
			$('#videoName').html(videoName); 
			$('#videoThemeTitle').html(themeTitle); 
			$('#videoDur').html(videoDuration ); 
			$('#videoDesc').html('<br/>'+videoDesc); 
			$('#FB_BOX').html(FB); 
			$('#videoComments').html(videoComments);	 

				$.getScript('js/facebook.js'); 
				initialize_google_plus(); 

			(function(){
				var flashvars = {affiliateSiteId:"194111", widgetId:"497897", width:"585", height:"308", revision:"50", mediaURL:thisVideo, autoPlay:true},
				params = {menu:"false", allowfullscreen:"true", allowscriptaccess:"always", wmode:"transparent"},
				attributes = {id:"kickWidget_194111_497897", name:"kickWidget_194111_497897"},
				expressInstallURL = "http://serve.a-widget.com/kickFlash/scripts/expressInstall2.swf?2",
				swfURL = "http://serve.a-widget.com/service/getWidgetSwf.kickAction",
				callback = function(e){ setTimeout('makeLoaderGoAway()',4000);
					
						if (e.success==false){				
						
						document.getElementById("altContentDiv_497897").innerHTML = '<iframe src="http://serve.a-widget.com/kickFlash/js/compiler/v2/iframe.html?affiliateSiteId=194111&amp;widgetId=497897&amp;widgetHost=serve.a-widget.com&amp;revision=50&amp;referralUrl='+escape(window.location.href)+'" height="308" width="585" frameborder="0" scrolling="no" style="border:none;overflow:hidden;width:652px;height:344px"></iframe>'; 
						} 
					}
				swfobject.embedSWF(swfURL, "altContentDiv_497897", "585", "308", "10", expressInstallURL, flashvars, params, attributes, callback);
			})();
			
			function makeLoaderGoAway(){
			
					$('.nloading').hide();  
			}
	
		hideLoader();	
		$(window).scrollTop($("#navContainer_innerBox").offset().top);

            }
			
        });
					
	 
	 
		

	
}


	
	
	
/*	Fills news page VIA ajax
 *	recieves NOTHING, uses preset varible
 */
function video_fillThemeIndex(){
	///alert("inside");
	//$('.nloading').show();
        var data = 'theme='+video_currentTheme+'&section='+video_currentSection;
        $.ajax({
            url: "include/video/video_Index_Ajax.php",	
            type: "GET",		
            data: data,		
            cache: false,
            success: function (html) {	
				//$('.vloading').hide();
                $('#catContent').html(html);
				
				
				//if this is the archive page
				if(thisPage == 'video-archive.php'){
					
					//if a theme was not selected get first themeID from page.
					if(video_currentTheme ==''){
						video_setCurrentTheme(document.getElementById('theme').value)
						//alert(document.getElementById('theme').value);
					}
				
					video_gotToArchive();
					$('.vloading').hide();				
					
					
				}else{
					
					video_fillVideos('');
					$('.vloading').hide();					
					
				}
                
            }		
        });
					
					
    }
	
function video_fillComments(){
	photo_Change_img_titles(2);
        var data = 'video='+video_currentVideo+'&theme='+video_currentTheme+'&year='+video_currentYear+'&month='+video_currentMonth+'&page='+video_currentCommentsPage+'&numPerPage='+video_NumPerPage+'&section='+video_currentSection;
        $.ajax({
            url: "include/video/videoComments_Ajax.php",	
            type: "GET",		
            data: data,		
            cache: false,
            success: function (html) {	
			$('.nloading').hide();
                $('#Ajax_Container').html(html);
				$("a[rel='box']").colorbox();
				//alert(html);
               // $(".viewProfilePages").colorbox({width:"80%", height:"80%", iframe:true});

				hideLoader();
				$(window).scrollTop($("#photo_content_img").offset().top);
            }		
        });
	}

function video_addComment(){
	showLoader();
	var c = $('textarea[name="comment"]');
	
	if (c.val()=='') {
            c.addClass('hightlight');
            return false;
        } else c.removeClass('hightlight');
         
       
		var data = 'c='+c.val()+'&video='+video_currentVideo;
        $.ajax({
            url: "include/video/videoAddComment_Ajax.php",	
            type: "GET",		
            data: data,		
            cache: false,
            success: function (html) {	
			//alert(html);
			video_fillComments();		
            }		
        });

}


/*	Fills news page VIA ajax
 *	recieves NOTHING, uses preset varible
 */
function video_fill_archive(caller){
	
        var data = 'theme='+video_currentTheme+'&video='+video_currentVideo+'&year='+video_currentYear+'&month='+video_currentMonth+'&section='+video_currentSection;;
        $.ajax({
            url: "include/video/videos_Archive_Ajax.php",	
            type: "GET",		
            data: data,		
            cache: false,
            success: function (html) {	
			// html = html.substring(0, html.length-1);
			$('.vloading').hide();
                $('#Ajax_Container').html(html);
				
               				
				if(caller == 'playVid'){
					
					playVideo();
				}else{
					hideLoader();	
				}
				
				
            }		
        });
					
					
    }
				

