﻿// JavaScript Document



var protected_load_url

$(function() {
		   
	
	
	if($("a[href^='/protected/']").size()>0) {
	   
	   preCheckAuthentication()
	}
	
	$("a[href^='/protected/']").click(function(event) {
									
									
		protected_load_url = this.href
		
		if(checkAuthTimeDif()) {location.href=protected_load_url}
		
		
		else {
			$.ajax({
			   
				url:"/ajax/protected.shtml",
				data:{ran:(new Date()).getTime()},
				success:handleProtectionAjax,
				failure:launchLogin,
				dataType:"xml"
				   
				   
			})
		}
		
		return false		
									
	})
	
	
	$("button.toggleVisibility").hover(
		function() {
			
			var butImg = $(this).find("img")
			butImg.css("positioning","relative")
			butImg.css("visibility","hidden")
		}
		,
		function() {
			var butImg = $(this).find("img")
			butImg.css("positioning","relative")
			butImg.css("visibility","visible")
		}
	)
	
	$("a.regLauncher").click(function(event) {
		
		var win = window.parent ? window.parent : window	
		win.launchRegister(this.href)
		event.preventDefault()
		
	})
	
	
	
	$(".apply-link").css("cursor","pointer").click(function(event) {
		
		var href=$(this).find("a").attr("href")
		
		if(href) {
			location.href=href
			event.preventDefault()
		}
		
	})
	
	
	
	$("a.demo-video").click(function(event) {
	
		
		$("div.video-wrapper img.placeholder").css("visibility","hidden")
		
		
		var data = new Object()
		data.activity_title = this.title ? this.title : $(this).text()
		data.activity_type = "video"
		
		
		data.request_path = this.pathname
		data.activity_info = location.pathname
		
		
		
		
		var callback = function (data, textStatus) {
			
			
			
		}
		
		$.post("/ajax/log_activity.shtml",data,callback)
		
		
		
		changeFLV(this.href)
		$(this).parents("ul").find("a").removeClass("current")
		$(this).addClass("current")
		
		
		
		event.preventDefault()
									 
	})
	
	
	$("a[href^='http']").click(function(event) {
		
		event.preventDefault()
		
		var data = new Object()
		data.activity_title = this.title ? this.title : $(this).text()
		data.activity_type = "external_link"
		
		
		data.request_path = this.href
		data.activity_info = location.pathname
		
		var aTag = this
		if(aTag.target=="_blank") {
			var newWin = window.open()
		}
		
		
		var callback = function (data, textStatus) {
			
			if(aTag.target=="_blank") {
				
				newWin.location.href=aTag.href	
				newWin.focus()
			}
			
			else {location.href=aTag.href}
			
		}
		
		$.post("/ajax/log_activity.shtml",data,callback)
								  
								  
	})
	
	
	$("div.video-wrapper img.placeholder").css("cursor","pointer").click(function() {
														   
		$("a.demo-video.overview").click()													   
														   
	})
	
	
	$("form.modal-form").submit(function() {
										 
		setTimeout(closeParentTB,2000)									 
										 
										 
	})
	
})


function closeParentTB() {
	
	if(window.parent != window) {
		try {window.parent.tb_remove()} 
		catch(e) {}
		try {window.parent.preCheckAuthentication()}
		catch(e) {}
	}
	
	
}


function checkAuthTimeDif() {
	
	if(!authenticated) {return false}
	else {
		var now = new Date()
		var timeDif = now.getTime()-authenticated.getTime()
		timeDif = timeDif/1000
		timeDif = timeDif/60
		if(timeDif<10) {return true}
		
		else {return false}
		
	}
	
	
}

function launchProtected() {
	location.href=protected_load_url
}



function tbPerc(obj) {
	
	var viewHeight = $(window).height()
	
	if(obj.percHeight) {
		obj.height = Math.round(viewHeight*obj.percHeight/100)
	}
	
	//alert(obj.height)
	
	if(obj.maxHeight) {
		if(obj.height>obj.maxHeight) {
			obj.height = obj.maxHeight
		}
	}
	
	tb_show("",obj.url+"?TB_iframe=true&amp;height="+obj.height+"&amp;width="+obj.width)
	
}

function launchRegister(url) {
	
	tb_remove()
	setTimeout(function() {tbPerc({url:url,percHeight:90,width:624,maxHeight:650})},500)
	
}

function changeFLV(flv) {
	
	var movie = document.flv_player ? 	document.flv_player : window.flv_player
	
	movie.changeFLV(flv)
	
	
}




function launchLogin() {
	
	tbPerc({url:protected_load_url,height:300,width:624})
	
}

function preCheckAuthentication() {
	
	
	$.ajax({
			   
		url:"/ajax/protected.shtml",
		data:{ran:(new Date()).getTime()},
		success:handleProtectionAjaxPre,
		dataType:"xml"
		   
		   
	})
}

function handleProtectionAjax(data) {
	
	if($("success",data).size()) {
		authenticated = new Date()
		launchProtected()
	}
	else {launchLogin()}
}


var authenticated

function handleProtectionAjaxPre(data) {
	
	if($("success",data).size()) {
	
	authenticated = new Date()
	
		
	}

}
