// JavaScript Document
$(document).ready(function() {
	//$.sifr({ path: '/fonts/', underline: true  });// this will prepend 'resources/fonts/' to the font name	
	//$('.post h2').sifr({ font: 'Museo300'});// this will load the font name 'My Awesome Font.swf'
	//console.log($('.post h2'));
	
	function getHash(a_sLink){
	   // Check for anchor and select active image from nav
	   var hash = 0;
	   if((a_sLink.match(/#(\w.*)/) != null)){
		 //if((isNumeric(a_sLink.match(/#(\w.*)/)[1]) == true)){
		   hash = a_sLink.match(/#(\w.*)/)[1];
		   //hash = hash.substr(0, hash.indexOf("?"));
		 //}
	  }
	  return hash;
	}
	
	var createElement = (function(){
		// Detect IE using conditional compilation
		if (/*@cc_on @*//*@if (@_win32)!/*@end @*/false)
		{
			// Translations for attribute names which IE would otherwise choke on
			var attrTranslations =
			{
				"class": "className",
				"for": "htmlFor"
			};
	
			var setAttribute = function(element, attr, value)
			{
				if (attrTranslations.hasOwnProperty(attr))
				{
					element[attrTranslations[attr]] = value;
				}
				else if (attr == "style")
				{
					element.style.cssText = value;
				}
				else
				{
					element.setAttribute(attr, value);
				}
			};
	
			return function(tagName, attributes)
			{
				attributes = attributes || {};
	
				// See http://channel9.msdn.com/Wiki/InternetExplorerProgrammingBugs
				if (attributes.hasOwnProperty("name") ||
					attributes.hasOwnProperty("checked") ||
					attributes.hasOwnProperty("multiple"))
				{
					var tagParts = ["<" + tagName];
					if (attributes.hasOwnProperty("name"))
					{
						tagParts[tagParts.length] =
							' name="' + attributes.name + '"';
						delete attributes.name;
					}
					if (attributes.hasOwnProperty("checked") &&
						"" + attributes.checked == "true")
					{
						tagParts[tagParts.length] = " checked";
						delete attributes.checked;
					}
					if (attributes.hasOwnProperty("multiple") &&
						"" + attributes.multiple == "true")
					{
						tagParts[tagParts.length] = " multiple";
						delete attributes.multiple;
					}
					tagParts[tagParts.length] = ">";
	
					var element =
						document.createElement(tagParts.join(""));
				}
				else
				{
					var element = document.createElement(tagName);
				}
	
				for (var attr in attributes)
				{
					if (attributes.hasOwnProperty(attr))
					{
						setAttribute(element, attr, attributes[attr]);
					}
				}
	
				return element;
			};
		}
		// All other browsers
		else
		{
			return function(tagName, attributes)
			{
				attributes = attributes || {};
				var element = document.createElement(tagName);
				for (var attr in attributes)
				{
					if (attributes.hasOwnProperty(attr))
					{
						element.setAttribute(attr, attributes[attr]);
					}
				}
				return element;
			};
		}
	})();

	
	function postToURL(url, values)
	{
		values = values || {};
	
		var form = createElement("form", {action: url,
										  method: "POST",
										  style: "display: none"});
		for (var property in values)
		{
			if (values.hasOwnProperty(property))
			{
				var value = values[property];
				if (value instanceof Array)
				{
					for (var i = 0, l = value.length; i < l; i++)
					{
						form.appendChild(createElement("input", {type: "hidden",
																 name: property,
																 value: value[i]}));
					}
				}
				else
				{
					form.appendChild(createElement("input", {type: "hidden",
															 name: property,
															 value: value}));
				}
			}
		}
		document.body.appendChild(form);
		form.submit();
		document.body.removeChild(form);
	}

	
	function isNumeric(sText)
	{
	  var validChars = "0123456789";
	  var isNumber=true;
	  var char;
	  for (i = 0; i < sText.length && isNumber == true; i++)
	  {
		char = sText.charAt(i);
		if (validChars.indexOf(char) == -1)
		{
		  isNumber = false;
		}
	  }
	  return isNumber;
	} 
	
	if(typeof sIFR == "function"){
		sIFR.replaceElement(".post h2", named({sFlashSrc: "/fonts/Museo300.swf", sWmode:"transparent", sBgColor: "#ffffff"}));
		sIFR.replaceElement(".authorpage h3", named({sFlashSrc: "/fonts/Museo300.swf", sWmode:"transparent", sBgColor: "#ffffff"}));
		sIFR.replaceElement("#latest-cat h2", named({sFlashSrc: "/fonts/Museo300.swf", sWmode:"transparent", sBgColor: "#ffffff"}));
	};
	
	function overlayformCallback(v,m,f){
		//if(v!="CancelButton"){
	    //  alert(v +' ' /*+ f.alertName*/);
		//}
		
		if (v=="SwitchToRegister"){
			window.location.hash = "showregister";
			showRegister();
		}
		
		if (v=="SwitchToLogin"){
			window.location.hash = "showlogin";
			showLogin();
		}
		
		if (v=="Login"){
			//alert('Loggin in: ' + f.frm_user_id);
			postToURL("/wp-login.php", {'log': f.frm_user_id, 'pwd': f.frm_user_pw});
		}
		
		if (v=="Register"){
			//alert('Loggin in: ' + f.frm_user_id);
			postToURL("/wp-login.php?action=register", {'user_login': f.frm_user_id, 'user_email': f.user_email, 'user_email': f.user_email, 'cimy_uef_FIRST_NAME': f.cimy_uef_1, 'cimy_uef_LAST_NAME': f.cimy_uef_2, 'cimy_uef_NEWSLETTER': f.cimy_uef_5, 'cimy_uef_AGREE': f.cimy_uef_6});
		}
		
		if (v=="ForgotPassword"){
			window.location.href = "/wp-login.php?action=lostpassword";
		}
	}
	
	function showLogin(){
		var msg = '';
		if ($("#tempmsg").text()!=""){
			msg = '<h3>'+$("#tempmsg").text()+'</h3>';
		}
		loginForm = msg+'<input type="text" id="frm_user_id" name="frm_user_id" /><input type="password" id="frm_user_pw" name="frm_user_pw" /><input type="submit" id="login_submit" value="login"/><span class="formlinks"><a title="Password Lost and Found" href="http://fip.solidflux.nl/wp-login.php?action=lostpassword">Forgot password?</a> | <a href="http://fip.solidflux.nl/wp-login.php?action=register">Register</a></span>';
		$.prompt(loginForm,{
			  	persistent: false, callback: overlayformCallback,
     			buttons: { Login: 'Login', 'Forgot Password': 'ForgotPassword', Cancel: 'CancelButton', Register: 'SwitchToRegister' }				
		}); 
		
		$("#login_submit").click(function () { 
			//console.log("loggin in");
			//console.log();
			postToURL("/wp-login.php", {'log': $("#frm_user_id").val(), 'pwd': $("#frm_user_pw").val()});
			return false;
		});
	}
	
	function showRegister(){
		var msg = '';
		if ($("#tempmsg").text()!=""){
			msg = '<h3>'+$("#tempmsg").text()+'</h3>';
		}else{
			msg = '<h3>Please fill out all the fields</h3>';
		}
		registerForm = msg+'<input type="text" name="frm_user_id" id="frm_user_id" value="" /><input type="text" name="user_email" id="user_email" value="" /><input type="hidden" name="cimy_post" value="1" /><input type="text" name="cimy_uef_FIRST_NAME" id="cimy_uef_1" value="" maxlength="500" /><input type="text" name="cimy_uef_LAST_NAME" id="cimy_uef_2" class="cimy_uef_input_27" value="" maxlength="500" /><input id="cimy_uef_9" class="cimy_uef_input_27" type="text" tabindex="23" maxlength="500" value="" name="cimy_uef_ACADEMIC_INSTITUTION"/><br /><input type="checkbox" name="cimy_uef_NEWSLETTER" id="cimy_uef_5" /><label class="cimy_uef_label_checkbox" for="cimy_uef_5"> I would like to receive the newsletter</label><br /><input type="checkbox" name="cimy_uef_AGREE" id="cimy_uef_6" value="1" checked="checked" /><label class="cimy_uef_label_checkbox" for="cimy_uef_6"> I agree with the <a href="/terms-and-services/">terms & services</a></label><input type="submit" id="register_submit" value="login"/>';
		$.prompt(registerForm,{
			  	persistent: false, callback: overlayformCallback,
     			buttons: { Register: 'Register', Cancel: 'CancelButton', Login: 'SwitchToLogin' }
		}); 
		
		$("#register_submit").click(function () { 
			//console.log("loggin in");
			//console.log();
			postToURL("/wp-login.php", {'log': $("#frm_user_id").val(), 'pwd': $("#frm_user_pw").val()});
			postToURL("/wp-login.php?action=register", {'user_login': $("#frm_user_id").val(), 'user_email': $("#user_email").val(), 'user_email': $("#user_email").val(), 'cimy_uef_FIRST_NAME': $("#cimy_uef_1").val(), 'cimy_uef_LAST_NAME': $("#cimy_uef_2").val(), 'cimy_uef_NEWSLETTER': $("#cimy_uef_5").val(), 'cimy_uef_AGREE': $("#cimy_uef_6").val()});
			return false;
		});
	}
	
	//console.log($('a#login'));
	
	if ($('.post-body a img').length>0){
		$('.post-body a img').parent().css("display", "block");
		$('.post-body a img').parent().css("textAlign", "center");
		$('.post-body a img').parent().css("border", "none");
	}
	
	if ($('div.wp-caption').length>0){
		$('div.wp-caption').css("width", "auto");
	}
		
	$('a#login').click( function () { 
		showLogin();
		return false;
	});
	
	$('a#register').click( function () { 
		showRegister();
		return false;
	});
	
	$('a.show-tags').click( function () { 
		if ($(this).text() == "hide tags"){
			//console.log($(this).prev().height());
			$(this).prev().fadeOut();	
			$(this).text("show tags");
		}else{
			//console.log($(this).prev().css('margin-top'));
			//console.log($(this).prev().height());
			if ($(this).prev().css('margin-top') == "-135px"){
				//$(this).prev().css('margin-top', (50  + parseInt($(this).prev().css('margin-top').replace('px', '')) + $(this).prev().height())+"px");
				$(this).prev().css('margin-top', '-'+($(this).prev().height()+25)+'px');
			}
			$(this).prev().fadeIn();
			$(this).text("hide tags");
		}		
		return false;
	});
	
	$('.faq h3').click( function () { 
		if ($(this).hasClass("open")){
			$(this).next().slideUp();
			$(this).removeClass("open");
			$(this).addClass("close");
		}else{
			$(this).next().slideDown();
			$(this).removeClass("close");
			$(this).addClass("open");
		}
	});
	
	$('.post-body a').click(function () { 
		if($(this).children("img").length==1){
			capObj = $(this).parent().find(".wp-caption-text");
			if (capObj.length!=0){
				caption = "<h3>"+capObj.html()+"</h3>";
			}else{
				caption = "";
			}
			
			//preloadimg = new Image();
			//$('img1').load
			//preloadimg.id = 'preload';
			//preloadimg.src=$(this).attr("href")
			//preloadimg.onload = imageLoaded();
			//console.log(img1.width); 

			
			$.prompt(caption+'<img src="'+$(this).attr("href")+'" />',{
			  	persistent: false, callback: overlayformCallback,
     			buttons: {}
			}); 
			
			$("#jqi .jqimessage img:first").load(function(){
				$("#jqi .jqimessage h3").css("width", $(this).width());
				//alert($(this).width());
			});
			
			//$("#jqi .jqimessage").prepend('<img src="'+$(this).attr("href")+'" style="display:none;"/>');
			//img = $("#jqi .jqimessage img");			
			return false;
		}

	});
	
	$('li.print').html('<a href="javascript:window.print();">Print this page<\/a>');

	//hash = getHash(document.location.href);
	//console.log(document.location.hash);
	switch(document.location.hash){
		case "#showlogin":
			showLogin();
			break; 
		case "#showregister":
			showRegister();
			break;
	}

	
});

