$(document).ready(function(){
	//MENU
	
	highlightMenu();
	
	var time_in_ms = 5000;
	
	$("#professional-market-training").mouseover(function(){
		$(".sub-menu-container").fadeIn();
	});

	$("#professional-market-training").mouseout(function(){	
		$(this).oneTime(time_in_ms, function(){
			$(".sub-menu-container").fadeOut();
		});
	});
	
	$(".sub-menu-container").mouseover(function() {
		$(".sub-menu-container").stopTime();
	});
	
	$(".sub-menu-container").mouseout(function(){	
		$(this).oneTime(time_in_ms, function(){
			$(".sub-menu-container").fadeOut();
		});
	});
	
	//$('.sidebar-login-content p').remove();
	
	$('.sidebar-login-content p').slice(2,1).remove();
	$("#wp-submit").attr("value", "");
	
	/*
	 * NEWS WIDGET
	 */
	 
	var currentAddress = window.location.pathname.substring(1, window.location.pathname.length);
	
	if(currentAddress != "news.php") {
	
		$(".close").hide();
		$(".close").click(function() {
				$(".sidebar-news-window").css("top", "0px");
				$(".sidebar-news-window").css("left", "0px");
				$(".sidebar-news-window").removeClass("sidebar-news-window-active");
				$(".close").hide();
		});
		
		$(".external").click(function() {
			var w = document.body.clientWidth, h = document.body.clientHeight;
			var popW = 270, popH = 270;
			
			//align popup in the middle
			//var leftPos = (w - popW) / 2, topPos = (h - popH) / 2;
			
			//align bottom right
			var leftPos = (w - popW), topPos = (h - popH);
			
			//window.open('', 'list','width=' + popW + ',height=' + popH + ',top=' + topPos + ', left=' + leftPos);
			
			var news_widget = window.open('/news.php', 'news_widget', 'width=270, height=270,top=' + topPos + ', left=' + leftPos + ', location=no, directories=no, menubar=no, resizable=no, scrollbars=no, status=no, toolbar=no, directories=no');
			
			//hide window from main website
			$(".sidebar-news-window").hide();
			
			//detect when popup is closed to reactivate the one on the websites
			var timer = setInterval(function() {   
		    	if(news_widget.closed) {  
		        	clearInterval(timer);  
		        	$(".sidebar-news-window").show();  
		    	}  
			}, 1000); 
		
		});
		
		$(".sidebar-news-window").draggable({
			cursorAt: { left: 0, top: 0 },
			start: function(event, ui) {
				$(".sidebar-news").addClass("sidebar-news-active");
			},
			stop: function(event, ui) {
				$(".sidebar-news").removeClass("sidebar-news-active");
			}
		});
		
		$( ".sidebar-news" ).droppable({
			drop: function(event, ui) {
				//action to do when dropped
				$(".sidebar-news-window").css("top", "0px");
				$(".sidebar-news-window").css("left", "0px");
				$(".sidebar-news-window").removeClass("sidebar-news-window-active");
				$(".close").hide();
			},
			out: function(event, ui) {
				$(".sidebar-news-window").addClass("sidebar-news-window-active");
				//$(".sidebar-news-window").css("border-style", "solid");
				$(".close").show();
			},
			over: function(event, ui) {
				//$(".sidebar-news-window").css("border-style", "dashed");
			},
			hoverClass: 'sidebar-news-drophover'
		});
		//$("#wp-submit").replace("Member Login", "");
		
		// News ticker
		$(".scrollable").scrollable({ vertical: true, mousewheel: true, circular: true }).autoscroll({ autoplay: true });
	}
});

$(function($) {
	var dst = 1;
	var gmt = new Date;
	var lsm = new Date;
	var lso = new Date;
	lsm.setMonth(2); // March
	lsm.setDate(31);
	
	var day = lsm.getDay();// day of week of 31st
	lsm.setDate(31-day); // last Sunday
	
	lso.setMonth(9); // October
	lso.setDate(31);
	
	day = lso.getDay();
	lso.setDate(31-day);
	
	if (gmt < lsm || gmt >= lso) dst = 0;
	
	//Clocks
	var options_nyc = {
		format: '%H:%M',
        utc: true,
        utcOffset: (-5 + dst)
    };
	
	var options_london = {
		format: '%H:%M',
        utc: true,
        utcOffset: (0 + dst)
    };
	
	var options_tokyo = {
		format: '%H:%M',
        utc: true,
        utcOffset: (9)
    };
	
	$("#clock-nyc").jclock(options_nyc);
    $('#clock-london').jclock(options_london);
    $("#clock-tokyo").jclock(options_tokyo);
});

function highlightMenu() {
	var currentAddress = window.location.pathname.substring(1, window.location.pathname.length);
	
	var selectedMenu = "";
		
	switch(currentAddress) {		
		case "fixed-income-flow":
			selectedMenu = "#" + currentAddress;
		break;

		case "the-squawk":
			selectedMenu = "#" + currentAddress;
		break;

		case "professional-market-training":
			selectedMenu = "#" + currentAddress;
		break;
		
		case "our-team":
			selectedMenu = "#" + currentAddress;
		break;

		case "news-and-press":
			selectedMenu = "#" + currentAddress;
		break;

		case "contacts":
			selectedMenu = "#" + currentAddress;
		break;
		
		default:
			if(window.location.pathname.length == 1) {
				selectedMenu = "#company-overview";
			}
	}
	
	$(selectedMenu).attr("class", "selected");
}

function sortFormFocus(id) {
	var currentForm = id;
	
	$(document).ready(function() {
		/*
		 * FOCUS
		 */
		$("#" + currentForm + "_fullname").focus(function() {
			if($(this).val() == "Name:") {
				$(this).val("");
			}
		});
		
		$("#" + currentForm + "_company").focus(function() {
			if($(this).val() == "Company:") {
				$(this).val("");
			}
		});
		
		$("#" + currentForm + "_email").focus(function() {
			if($(this).val() == "Email:") {
				$(this).val("");
			}
		});
		
		$("#" + currentForm + "_phone").focus(function() {
			if($(this).val() == "Telephone:") {
				$(this).val("");
			}
		});
		
		$("#" + currentForm + "_message").focus(function() {
			if($(this).val() == "Message (Optional):") {
				$(this).val("");
			}
		});
		
		/*
		 * BLUR
		 */
		$("#" + currentForm + "_fullname").blur(function() {
			if($(this).val() == "") {
				$(this).val("Name:");
			}
		});
		
		$("#" + currentForm + "_company").blur(function() {
			if($(this).val() == "") {
				$(this).val("Company:");
			}
		});
		
		$("#" + currentForm + "_email").blur(function() {
			if($(this).val() == "") {
				$(this).val("Email:");
			}
		});
		
		$("#" + currentForm + "_phone").blur(function() {
			if($(this).val() == "") {
				$(this).val("Telephone:");
			}
		});
		
		$("#" + currentForm + "_message").blur(function() {
			if($(this).val() == "") {
				$(this).val("Message (Optional):");
			}
		});
	});
}

function sendData(form) {
	$("." + form + "-registration-processing").show();
	$("." + form + "-registration-processing").animate({
	    width: '214px'
	}, 1000, function() {
	    // Animation complete activate the rest.
		processData(form);
	});
}

function processData(form) {
	var filter = /^\s*[\w\-\+_]+(\.[\w\-\+_]+)*\@[\w\-\+_]+\.[\w\-\+_]+(\.[\w\-\+_]+)*\s*$/;
    //return String($("#"+ form +"_email").val()).search (filter) != -1; // returns false or true;

	$("." + form + "-registration-processing").html('Checking Details...');
	
	if( ($("#"+ form +"_fullname").val() == "Name:") || ($("#"+ form +"_fullname").val() == "") ) {
		$("." + form + "-registration-processing").addClass("form-error").html('Please insert your name');
		$("#"+ form +"_fullname").focus();
		return false;
	} else if ( ($("#"+ form +"_email").val() == "Email:") || ($("#"+ form +"_email").val() == "") || ((String($("#"+ form +"_email").val()).search (filter) != -1) == false) ) {
		$("." + form + "-registration-processing").addClass("form-error").html('Please insert your email');
		$("#"+ form +"_email").focus();
		return false;
	} else if ( ($("#"+ form +"_phone").val() == "Telephone:") || ($("#"+ form +"_phone").val() == "") ) {
		$("." + form + "-registration-processing").addClass("form-error").html('Please insert your phone number');
		$("#"+ form +"_phone").focus();
		return false;
	}
	
	$("." + form + "-registration-processing").removeClass("form-error");
	$("." + form + "-registration-processing").html('Sending Details...');
	
	deliverData(form);
}

function deliverData(form) {
    var fullUrl = _templateurl + "/send_form.php";
    var currentForm = $("#" + form + "_form");
    
    $.post (
    		fullUrl,
    		currentForm.serialize(),
    		function(data) {
				if(data.message == "success") {
					$("." + form + "-registration-processing").html('');
					$("." + form + "-registration-processing").html(' Message received. Thank you!');
				}
    		},
    		"json"
    );
}
