/*
 * Javascript file that for the video page
 * @author Scott Shaper
 */

//GLOBAL VARIBLES
var databaseDelayMsg = "<p>RETRIEVING DATA...</p>";


$(document).ready(function(){

    $('#highestrated').hide();
    $('#education').hide();
    $('#athletic').hide();
    $('#campus').hide();
	$('#search').hide();
    addCalendar();//causes the small calendar to display for the date boxes when the page reloads
	//following two lines take the id from the get statement and send ajax request to the database to update the page ranking. This was added on 08/20/2009 to compensate for having all front end pages on a read only database.
	var id = $('#hiddenid').val();
    $.post("admin/process/increase_video_rank_proc.php",{id:id});

    //checks for sub group
    //checksubgroup();  06/30/2010 THIS FUNCTION NO LONGER NEEDED.
	
});


/*
 * 06/30/2010 THIS FUNCTION NO LONGER NEEDED.
 *
function checksubgroup()
{
    var id = $('#hidid').val();
    var subgroup = $('#hidsubgroup').val();
    if (subgroup == "y")
        {
            var placeHolderPic = $('#hidplaceholderpic').val();
            var content = '<a href="video_subset?m='+id+'"><img src="images/seriesimage_496.png" alttag="series" /></a>';
            $('#videocontent').html(content);
        }
    else
        {
            $('#videocontent').html('<a href="http://www.adobe.com/go/getflashplayer"><img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash player" /></a>')
        }
}
*/

function listView(){
	//$("#videotabs").load("admin/includes/list_tabs.php");
        var type = "L";
        $.post("admin/process/tabs_proc.php",{type:type},function(data){$('#videotabs').html(data);});
	$("#videoselections").html('');
	$("#videoselections").hide();
	$(".searchdetails").show();

        $('.searchdetails').html(databaseDelayMsg);

	changeTabList(1,'recent',false);
}

function listAdvancedSearch(){
	//$("#videotabs").load("admin/includes/list_tabs.php");
        var type = "L";
        $.post("admin/process/tabs_proc.php",{type:type},function(data){$('#videotabs').html(data);});
	$("#videoselections").html('');
	$("#videoselections").hide();
	$(".searchdetails").show();

        $('.searchdetails').html(databaseDelayMsg);

	changeTabList(1,'search',true);

        //advancedSearch(p,a);

}


function imageView(){
	//$("#videotabs").load("admin/includes/image_tabs.php");
        var type = "P";
        $.post("admin/process/tabs_proc.php",{type:type},function(data){$('#videotabs').html(data);});
        $(".searchdetails").html('');
	$(".searchdetails").hide();
	$("#videoselections").show();

        $('#videoselections').html(databaseDelayMsg)
	changeTab('recent');
}


function getPage(a,b){
	$.post("admin/process/video_tabs_list.php",{a:a,b:b},function(data){$('.searchdetails').html(data);});
}

function getPageSearchKeyword(p,a,k){
    //p is the page number
    //a is the type (keyword or date)
    //k is the search term
	$.post("admin/process/search.php",{p:p,a:a,k:k},function(data){$('.searchdetails').html(data);});
}
function getPageSearchDate(p,a,sd,ed){
    //p is the page number
    //a is the type (keyword or date)
    //sd is the startDate
    //ed is the endDate
    $.post("admin/process/search.php",{p:p,a:a,sd:sd,ed:ed},function(data){$('.searchdetails').html(data);addCalendar();});
    //addCalendar() enables the small calendar to display
}


function changeTab(a){
	//a = tab name
	$('#tab_recent').removeClass();
	$('#tab_popular').removeClass();
	$('#tab_education').removeClass();
	$('#tab_athletic').removeClass();
	$('#tab_campus').removeClass();
	
	if (a == 'recent'){$('#tab_recent').addClass('here');}
	else if (a == 'popular'){$('#tab_popular').addClass('here');}
	else if (a == 'education'){$('#tab_education').addClass('here');}
	else if (a == 'athletic'){$('#tab_athletic').addClass('here');}
	else if (a == 'campus'){$('#tab_campus').addClass('here');}
	
	
	$.post("admin/process/video_tabs.php",{a:a},function(data){$('#videoselections').html(data);});	
}

function changeTabList(a,b,c){
    //a = page number which is one
    //b = the tab name
    //c = boolean determines of a callback to advances search is needed or not.
	$('#tab_recent').removeClass();
	$('#tab_popular').removeClass();
	$('#tab_education').removeClass();
	$('#tab_athletic').removeClass();
	$('#tab_campus').removeClass();
	$('#tab_search').removeClass();
        $('#tab_list').removeClass();

	
	if (b == 'recent'){$('#tab_recent').addClass('here');}
	else if (b == 'popular'){$('#tab_popular').addClass('here');}
	else if (b == 'education'){$('#tab_education').addClass('here');}
	else if (b == 'athletic'){$('#tab_athletic').addClass('here');}
	else if (b == 'campus'){$('#tab_campus').addClass('here');}
	else if (b == 'search'){$('#tab_search').addClass('here');}
        else if (b == 'list'){$('#tab_list').addClass('here');}
	
	if(c)
            {
                $.post("admin/process/video_tabs_list.php",{a:a,b:b},function(data){$('.searchdetails').html(data);advancedSearch(a,b);});
            }
        else
            {
                $.post("admin/process/video_tabs_list.php",{a:a,b:b},function(data){$('.searchdetails').html(data);});
            }
		
}

function advancedSearch(p,a){
    //p = page number which is one by default
    //a = the tab name
	$('#tab_recent').removeClass();
	$('#tab_popular').removeClass();
	$('#tab_education').removeClass();
	$('#tab_athletic').removeClass();
	$('#tab_campus').removeClass();
	$('#tab_search').removeClass();
        $('#tab_list').removeClass();

       	if (a == 'recent'){$('#tab_recent').addClass('here');}
	else if (a == 'popular'){$('#tab_popular').addClass('here');}
	else if (a == 'education'){$('#tab_education').addClass('here');}
	else if (a == 'athletic'){$('#tab_athletic').addClass('here');}
	else if (a == 'campus'){$('#tab_campus').addClass('here');}
	//else if (a == 'search'){$('#tab_search').addClass('here');}
        else if (a == 'search'){$('#tab_search').addClass('here');}

        a = "keyword"//this allows for the search script to be universal.  Modified on 11/5/2009
	
	$.post("admin/process/search.php",{p:p,a:a},function(data){$('.searchdetails').html(data);});
}


function searchCriteria(){
    var a = $('#criteria').val();
    var b = "";
	if (a == "keyword"){
		
		/*
		b += '<div id="searchArea>"'
				b += '<table>';
				b += '<tr><td class="cellWidth"><label for="criteria">Select Search Type:</label></td><td><span id="labelText"><label for="keyword">Enter Keyword</label></span></td></tr>';
				b += '<tr><td class="cellPadding"><select name="criteria" id="criteria" onchange="javascript:searchCriteria()">';
				b += '<option value="keyword" selected="selected">Keyword Search</option>';
				b += '<option value="date">Date Search</option>';
				b += '</select></td>';
				b += '<td class="cellPadding"><input name="keywords" id="keyword" type="text" size="35" />';	
				b += '<input id="searchBtn" name="Search" type="button" value="Search" onclick="javascript:searchVideos(\'keyword\');" /></td></tr>';
				b += '</table>';
				b += '</div>'
		*/
		
		
		
		b+= '<label for="criteria">Select Search Type</label> ';
		
		b+= '<select name="criteria" id="criteria" onchange="javascript:searchCriteria()">';
		b+= '<option value="keyword" selected="selected">Keyword Search</option>';
		b+= '<option value="date">Date Search</option>';
		b+= '</select>';
		
		b+= ' <label for="keyword">Enter Keyword</label> ';
		b+= ' <input name="keywords" id="keyword" type="text" size="35" />';	
		b+= '<input id="searchBtn" name="Search" type="button" value="Search" onclick="javascript:searchVideos(\'keyword\');" />';
		
		$('#searchArea').html(b);
		$('#keyword').focus();
		
		
	/*
		b = ' <input name="keyword" id="keyword" type="text" value="Type Keyword to Find Videos" size="35" /> ';
			b += ' <input name="Search" type="button" value="Search" onclick="javascript:searchVideos(\'keyword\');" />';
			c='<label for="keyword">Enter Keyword</label>';
			$('#searchCriteria').html(b);
			$('#labelText').html(c);*/
	
		//$('#keyword').focus();
	}
	else if (a == "date"){
		
		
		b+= '<label for="criteria">Select Search Type</label> ';
		
		b+= '<select name="criteria" id="criteria" onchange="javascript:searchCriteria()">';
		b+= '<option value="keyword">Keyword Search</option>';
		b+= '<option value="date" selected="selected">Date Search</option>';
		b+= '</select>';
		
		b+= '<label for="stDate">Enter Start Date</label> ';
		b+= ' <input name="stDate" id="stDate" class="datepicker" type="text" size="10" /> ';
		b+= ' <label for="edDate">Enter End Date</label> ';
		b+= ' <input name="edDate" id="edDate" class="datepicker" type="text" size="10" /> ';
		b+= ' <input id="searchBtn" name="Search" type="button" value="Search" onclick="javascript:searchVideos(\'date\');" />';
		
		$('#searchArea').html(b);
		addCalendar();
		$('#stDate').focus();
		
		
		
		/*
		b = ' <input class="datepicker" name="stDate" id="stDate" type="text" size="10" /> <input class="datepicker" name="edDate" id="edDate" type="text" size="10" /> ';
				b += ' <input name="Search" type="button" value="Search" onclick="javascript:searchVideos(\'date\');" />';
				c = '<label for="stDate">Enter Starting and Ending Date</label>';
				$('#searchCriteria').html(b);
				$('#labelText').html(c);*/
		
	}
}

function searchVideos(a){
	if (a == "keyword"){
		var k = $('#keyword').val();
        if (k == ""){
            alert("You must enter a keyword to search for");
            $('#keyword').focus();
        }
        else{
           $.post("admin/process/search.php",{a:a,k:k},function(data){$('.searchdetails').html(data);});
        }
			
	}
	else if (a == "date"){
		var sd = $('#stDate').val();
		var ed = $('#edDate').val();
		if (sd == "Start Date" || ed == "End Date"){
			alert("You must enter a date range");
       	}
		else{
			$.post("admin/process/search.php",{a:a,sd:sd,ed:ed},function(data){$('.searchdetails').html(data);addCalendar();});
		}
	}
}

function addCalendar(){
	$(".datepicker").datepicker();//Function that creates the datepicker ui	
}

function clearText(a){
	//a=box to clear
	$('#'+a).val('');
}
