/************************************************************************************************************************
vtccontrols.js || Menu control as well as ajax Control
------------------------------------------------------------------------------------------------------------------------
Developed By Cory Caines || Kreated Nine Web Development
************************************************************************************************************************/
var slist = new Array();
var selected = null;

var cat_opened = new Image();
cat_opened.src = "../images/vtc/cat_arr_down.gif";


function init_vtc_menu(){
	xlist = tag("div");
	listnum = 0;
	for(x=0;x<=xlist.length-1;x++){	
		listsrc = $("sub_"+listnum);
		if(listsrc != null){
			listsrc.style.overflow = "hidden";
			listsrc.style.height = "0px";
			listnum++;
		}
	}
	$("sel_type").value = "myvtc";
}

function vtc_toggle(loc,cid,title,typ){
	listsrc = $("sub_"+loc);

	//close any opened category if its not the currently selected one
	
	if(selected != loc && selected != null){
		$("sub_"+selected).style.height="0px";
		$("cattd_"+selected).className = "";
		$("img_"+selected).src = "../images/vtc/cat_closed.gif";
	}
		
	if(listsrc.style.height == "0px"){
		//open the sub category
		listsrc.style.height="auto";
		
		$("img_"+loc).src = cat_opened.src;
		$("cattd_"+loc).className = "spacer";
		
		//load all of the videos for the selected category
		loadVid(cid,cid,title,typ);
	}else{
		//just close the select caegory
		listsrc.style.height="0px";
		$("cattd_"+loc).className = "";
		$("img_"+loc).src = "../images/vtc/cat_closed.gif";
	}
	selected = loc;
}
//function for scrolling the div
function scrollMenu(dwnBtn,step,dir){
	dwnBtn.blur();
	var menu = $("menu");
	if(dir == "up"){
		menu.scrollTop -= step;
	}else{
		menu.scrollTop += step;
	}
}
function scrollDownHold(dwnBtn,step){
	holdDown = setInterval("scrollDown('"+dwnBtn+"','"+step+"')",500);
}

//load the clips from a category
function loadVid(cid,sid,title,typ){
//clips loaded the the category list
//resent all the clips variables to defualt
	$("clipContent").innerHTML = "<img src=\"../images/load.gif\" />";
	$("cid").value = cid;
	$("sid").value = sid;
	$("curPage").value= 0;
	$("totalPage").value= 0;
	$("totalclips").value= 0;
	
	$("curpage").innerHTML= 0;
	$("totalpage").innerHTML= 0;
	
	//set the title thats being loaded
	$("sel_title").value = title;
	$("sel_load_type").value = typ;
	
	var sel_type = $("sel_type").value;
	var url="../ajax/getvideodata.php?cid="+cid+"&sid="+sid+"&typ="+sel_type+"&load_typ="+typ;
	window.setTimeout("doload('"+url+"')",1000); 
}		

function doload(url){
	req=makeRequest();
	if (req==null){
		msg("Browser does not support HTTP Request")
		return
	} 

	req.onreadystatechange=readClips;
	req.open("GET",url,true)
	req.send(null)	
}
//retreive the clip data and display the videos
function readClips(){
	switch (req.readyState) {
		case 1 : 
		break;
		case 4 :
			if (req.status == 200) {
				var sel_type = $("sel_type").value;
				//clear all the info
				$("clipContent").innerHTML = "";
				$("bundleContent").innerHTML = "";
				switch(sel_type){
					case "myvtc":
						data = req.responseText;
						data = data.split("*");
						var info = data[0];
						//set the information
						info = info.split("|");
						var total_clips = info[0];
						var cur_page = info[1];
						var total_page = info[2];
						var total_have_cnt = info[3];
						var total_clip_cnt = info[4];
						
						set_page_info(total_clips,cur_page,total_page,total_have_cnt,total_clip_cnt);
						
						var sel_load_type = $("sel_load_type").value;
						if(sel_load_type == "clips"){
							data = data[1];
							clips = data.split("~"); // split into clips
						
							//loop through the clips
							var cc = $("clipContent");
							cc.innerHTML = "";
							
							if(clips != ""){
								for(i=0;i<=clips.length-1;i++){
									//split into current clip information $vid|$name|$img|$views
									clip_data = clips[i].split("|");
									var vid = clip_data[0];
									var name = clip_data[1];
									var img = clip_data[2];
									var views = clip_data[3];
									var cat1 = clip_data[4];
									var cat2 = clip_data[5];					
									csrc = "<div class=\"clipBox\">";
										csrc += "<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" style=\"width:100%;height:160px;\">";
											csrc += "<tr>";
												csrc += "<td style=\"height:140px;\" valign=\"top\">";
													csrc += "<div class=\"clipName\" align=\"left\">"+name+"</div>";
													csrc += "<div align=\"center\">";
													csrc += "<a href=\"javascript:reqVid("+vid+");\">";
														csrc += "<img src=\""+img+"\" border=\"0\" />";
													csrc+= "</a>";
													csrc += "</div>";
													csrc += "<div class=\"clipCats\" align=\"left\">";
														csrc += "<div>"+cat1+"</div>";
														csrc += "<div>"+cat2+"</div>";
													csrc += "</div>";
												csrc += "</td>";
											csrc += "</tr>";	
											csrc += "<tr>";
												csrc += "<td align=\"right\" style=\"padding-right:5px;\">";
													csrc += "<a href=\"javascript:reqVid("+vid+");\" class=\"play\">PLAY</a>";
												csrc += "</td>";
											csrc += "</tr>";
										csrc += "</table>";
									csrc += "<div>";
									cc.innerHTML += csrc;
								}
							}else{
								csrc = "<h2>Sorry You Do Not Have Any Clips To View In This Category</h2>";	
								cc.innerHTML = csrc;
							}
						}else{
							//load the defualt text;
							$("clipContent").innerHTML = "<h2>Select the category for the techniques you wish to view</h2>";
						}
					break;
					case "purchased":
					
					case "download":
						data = req.responseText;

						data = data.split("*");
						info = data[0];
						//set the information
						info = info.split("|");
						var total_clips = info[0];
						var cur_page = info[1];
						var total_page = info[2];
						var total_have_cnt = info[3];
						var total_clip_cnt = info[4];
						set_page_info(total_clips,cur_page,total_page,total_have_cnt,total_clip_cnt);
						//bundles
						bundles = data[1];
						if(bundles != null && bundles != "" && bundles != " "){
							bundle_data = bundles.split("|");
							bid = bundle_data[0];
							name = bundle_data[1];
							details = bundle_data[2];
							price = bundle_data[3];
							num= bundle_data[4];
							//check if the user is logged in
							if(name != null && name != " " && name != ""){
								if($("log").value == "true"){
									bsrc = "Download techniques: ";
									bsrc +=" <a href=\"../mycart/index.php?act=add&typ=bundle&id="+bid +"\" target=\"_self\">";
										bsrc +="<img src=\"../include/button.php?btn="+name+"\" border=\"0\" />";
									bsrc +="</a>";
								}else{
									bsrc = "Download techniques:";
									bsrc +=" <a href=\"javascript:void(0);\">";
										bsrc +="<img src=\"../include/button.php?btn="+name+"\" border=\"0\" ";
											bsrc+="onclick=\"vtcAlert('NOT_LOGGED')\" />";
									bsrc +="</a>";
								}
								//add the whats this image
								bsrc +=" <a href=\"javascript:void(0);\">";
									bsrc +="<img src=\"../images/buttons/btn_whatsthis.gif\" border=\"0\" ";
									bsrc += "onmouseover=\"wi_over(event);\" id=\""+bid+"\" oncick=\"this.blur();\" />";
								bsrc +="</a>";
																
								$("bundleContent").innerHTML +=" "+bsrc;
							}
						}
						var sel_load_type = $("sel_load_type").value;
						if(sel_load_type == "clips"){
							//data for all the clips being loaded
							data = data[2];
							clips = data.split("~"); // split into clips
							//loop through the clips
							var cc = $("clipContent");
							cc.innerHTML = "";
							
							if(clips != ""){
								for(i=0;i<=clips.length-1;i++){
									//split into current clip information $vid|$name|$img|$views
									
									clip_data = clips[i].split("|");
									var vid = clip_data[0];
									var name = clip_data[1];
									var img = clip_data[2];
									var views = clip_data[3];
									var type = clip_data[4];
									var cat1 = clip_data[5];
									var cat2 = clip_data[6];	
									filter = "";
									if(type != "have"){
										filter = "filter";
									}
									csrc = "<div class=\"clipBox "+filter+"\">";
										csrc += "<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" style=\"width:100%;height:160px;\">";
											csrc += "<tr>";
												csrc += "<td style=\"height:140px;\" valign=\"top\">";
													csrc += "<div class=\"clipName\" align=\"left\">"+name+"</div>";
													csrc += "<div align=\"center\">";
													if(type == "free" || type == "have"){
														csrc += "<a href=\"javascript:reqVid("+vid+");\">";
													}
														csrc += "<img src=\""+img+"\" border=\"0\" />";
													if(type == "free" || type == "have"){
														csrc += "</a>";
													}
													csrc += "</div>";
													csrc += "<div class=\"clipCats\" align=\"left\">";
														csrc += "<div>"+cat1+"</div>";
														csrc += "<div>"+cat2+"</div>";
													csrc += "</div>";
												csrc += "</td>";
											csrc += "</tr>";	
											csrc += "<tr>";
												csrc += "<td align=\"right\" style=\"padding-right:5px;\">";
												if(type == "free" || type == "have"){
													csrc += "<a href=\"javascript:reqVid("+vid+");\" class=\"play\">PLAY</a>";
												}
												csrc += "</td>";
											csrc += "</tr>";
										csrc += "</table>";
									csrc += "<div>";
									cc.innerHTML += csrc;
								}
							}else{
								csrc = "<h2>Sorry You Do Not Have Any Clips To View In This Category</h2>";	
								cc.innerHTML = csrc;
							}
						}else{
							//load the defualt text
							$("clipContent").innerHTML = "<h2>Select the category for the techniques you wish to view</h2>";
						}	
					break;
				}
	
			}else{ 
				csrc = "<h2>An Error Occured When Retrieving the Video Clip data</h2>";
				cc.innerHTML = csrc;
			}
		break;
	}	
}

function reqVid(id){
	var cid = $("cid").value;
	var mdisplay = $("mdisplay");
	//move page to top of vtc
	jump("vtc_top");
	mdisplay.src = "vtcplayer.php?vid="+id+"&cid="+cid;
}

/*Next and Previous Buttons */
function set_page_info(total_clips,cur_page,total_page,total_have_cnt,total_clip_cnt){
	$("curPage").value=cur_page;
	$("totalPage").value=total_page;
	$("totalclips").value=total_clips;
	
	//selected title
	var title = $("sel_title").value;
	
	$("clip_info").innerHTML = title+" - You have "+total_have_cnt+"/"+total_clip_cnt+" Available Techniques";
	if($("sel_load_type").value == "clips"){	
		$("curpage").innerHTML = cur_page;
		$("totalpage").innerHTML = total_page;
	}
}
function c_next(btn){
	btn.blur();
	
	cur_page = $("curPage").value;
	total_page = $("totalPage").value;
	
	if(cur_page == total_page){
		
	}else{
		$("clipContent").innerHTML = "<img src=\"../images/load.gif\" />";
		// goto the next page
		var cid = $("cid").value;
		var sid = $("sid").value;
		var sel_type = $("sel_type").value;
		
		var sel_load_type = $("sel_load_type").value;
		//send cur page to
		var url="../ajax/getvideodata.php?cid="+cid+"&sid="+sid+"&pg="+cur_page+"&typ="+sel_type+"&load_typ="+sel_load_type+"&pgt=nx";
		window.setTimeout("doload('"+url+"')",500); 
	}
}

function c_prev(btn){
	btn.blur();
	
	cur_page = $("curPage").value;
	
	if(cur_page != 1){	
		$("clipContent").innerHTML = "<img src=\"../images/load.gif\" />";
		//can goto the next page
		var cid = $("cid").value;
		var sid = $("sid").value;
		var sel_type = $("sel_type").value;
		//send cur page to
		var sel_load_type = $("sel_load_type").value;
		var url="../ajax/getvideodata.php?cid="+cid+"&sid="+sid+"&pg="+cur_page+"&typ="+sel_type+"&load_typ="+sel_load_type+"&pgt=px";
		window.setTimeout("doload('"+url+"')",1000); 
	}
}


/**/

/*
Functions for the VTC tabs
*/
function reset_vtc(){
	
	//clear all the clip content
	$("clipContent").innerHTML = "";
	$("clip_info").innerHTML = "";
	$("bundleContent").innerHTML = "";
	//clear the page numbering data
	$("curpage").innerHTML = "0";
	$("totalpage").innerHTML = "0";
	$("cid").value="";
	$("sid").value="";
	$("curPage").value="";
	$("totalPage").value="";
	$("totalclips").value="";
	$("sel_load_type").value ="";
	//close the categories
	if(selected != null){
		$("sub_"+selected).style.height="0px";
		$("cattd_"+selected).className = "";
		$("img_"+selected).src = "../images/vtc/cat_closed.gif";
	}
}

tab_list = Array();
function init_vtc_tabs(){
	//build the mouse commands for the 
	tabs = tag("div");
	for(i=0;i<=tabs.length-1;i++){
		if(tabs[i].getAttribute('typ') == "tab"){
			tab_list.push(tabs[i].id);
			//check it the tab is enabled of disables
			var enabled = tabs[i].getAttribute("enabled");
			if(enabled == "false"){
				tabs[i].onclick = function(){
					content = "You must be logged in to view this section.<br/> If you have an account then <a href=\"../accounts/login.php\"target=\"_self\">login</a> or <a href=\"../accounts/register.php\">Sign up</a> for a free account";
					alertCustom("Please Login",content,"OK");
				}
			}else if(enabled == "true"){
				tabs[i].onclick = function(){
					for(x=0;x<=tab_list.length-1;x++){
						$(tab_list[x]).className = "tab_off";	
					}
					this.className = "tab_sel";
					$("sel_type").value = this.id;
					reset_vtc();
				}
				tabs[i].onmouseover = function(){
					if(this.className == "tab_off"){
						this.className = "tab_ovr";
					}
				}
				tabs[i].onmouseout = function(){
					if(this.className == "tab_ovr"){
						this.className = "tab_off";
					}
				}
			}//end enabled check
		}
	}
}
function sim_tab_click(sim_tab){
	for(x=0;x<=tab_list.length-1;x++){
		$(tab_list[x]).className = "tab_off";	
	}
	$(sim_tab).className = "tab_sel";
	$("sel_type").value = $(sim_tab).id;
	reset_vtc();
}

function vtcAlert(action){
	switch(action){
		case "NOT_LOGGED":
			title = "Please Login";
			content = "You must be logged in to Purchase This Bundle.<br/> If you have an account then <a href=\"../accounts/login.php\" target=\"_self\">login</a> or <a href=\"../accounts/register.php\">Sign up</a> for a free account";
			//send the to custon Alert function to show
			alertCustom(title,content,"OK");
		break;
	}
}

function makeRequest(){ 
	var objXMLHttp=null;
	
	if (window.XMLHttpRequest){
	  objXMLHttp=new XMLHttpRequest()
	}else if(window.ActiveXObject){
		objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP")
	}
	return objXMLHttp
}