// Code for tracking page load times. Being used by the banner auto-loader.
var pageLoadBegin = (new Date()).getTime();
function getCurrentLoadTime() {
	currTime = (new Date()).getTime();
	return currTime - pageLoadBegin;
}

var defValue = 'Search';
var inputWidth = '120px';
var inputWidthReturn = '120px';
$(document).ready(function() {
	$('#site_search').focus(function(){
		if($(this).val() == defValue){
			$(this).val('').animate({
				width: inputWidth
			}, 400 )
		}
	});
	$('#site_search').blur(function(){
		if($(this).val().length == 0){
			$(this).val(defValue).animate({
				width: inputWidthReturn
			}, 500 )
		}
	});
	$('#site_search').val(defValue);
});

$(document).ready(function() {
	/* Product Expandables */
	$('p.ck-exp-title').click(function () {
	    $(this).toggleClass('expanded').next('p.ck-exp-text').slideToggle(400);
	});
	
	$('a.ajax-dialog').click(function(e) {
		var jLink = $(this);
		
		fioAjaxDialog(jLink.attr('href'), jLink.attr('title'));
		
		//prevent the browser to follow the link
		e.preventDefault();
	});
	
	$('form.fusion-login').live('submit', function() {
		var jFormElement = $(this);
		
		$.post('/account/login', jFormElement.serialize(), function(data) {
			var responseCode = data.code;
			
			// Check for success code
			if (responseCode >= 200 && responseCode < 300)
			{
				var callback = jFormElement.data('callback')
				
				// Call callback before redirecting
				if (typeof callback != 'undefined' && $.isFunction(callback))
				{
					var callbackParam;
					
					if ($('#targetWebinarUrl').length)
					{
						callbackParam = $('#targetWebinarUrl').val();
					}
					else
					{
						callbackParam = function() {
							window.location = document.URL.replace("http://","https://");
						};	
					}
					
					$('#fio-dialog').dialog('close');
					callback(callbackParam);
				}
				else
				{
					window.location = document.URL.replace("http://","https://");
				}
			}
			else
			{
				jFormElement.siblings('.error:first').html(data.message);
			}
		}, 'json');
		
		return false;
	});
});

function fioAjaxDialog(url, title, callback) {
	var jDialog = $('<div id="fio-dialog" style="display:hidden"></div>').appendTo('body');
	
	// load remote content
	jDialog.load(url, {"referrer": location.pathname}, function (responseText, textStatus, XMLHttpRequest) {
		if ($.isFunction(callback))
		{
			callback();
		}
		
		jDialog.dialog({
			closeOnEscape: true,
			draggable: true,
			modal: true,
			resizable: false,
			position: 'center',
			title: title,
			close: function(ev, ui) {
				$(this).remove();
			}
		});
	});
}

function fioDialog(title, content, callback) {
	var jDialog = $('<div id="fio-dialog" style="display:hidden">'+content+'</div>').appendTo('body');
	
	jDialog.dialog({
		closeOnEscape: true,
		draggable: true,
		modal: true,
		resizable: false,
		position: 'center',
		title: title,
		close: function(ev, ui) {
			$(this).remove();
			
			if ($.isFunction(callback))
			{
				callback();
			}
		}
	});
}
