$(document).ready(function(){
	var lang = $("body").attr("class");
	if($("ul.other").length && $("p.navigation a").length) {
		
		//var category = $("p.navigation a").text() + " ";
		if(lang == "bg") {
			var left_quote = "„";
			var right_quote = "“";
		} else if(lang == "en") {
			var left_quote = "“";
			var right_quote = "”";
		}
		
		$("ul.other li a").each(function (i, item) {
			var old_str = $(item).text();
			var s = old_str.indexOf(left_quote);
			//var new_str = old_str.replace(category, "");
			if(s != -1) {
				var new_str = old_str.substring(s+1, old_str.length);
				new_str = new_str.replace(right_quote, "");
				$(item).text(new_str);
			}
		});
		
		if($("ul.other li.current_page_item").prev().html() != null) {
			var prev = "<li class='prev'>" + $("ul.other li.current_page_item").prev().html() + "</li>";
			var prev_title = $("ul.other li.current_page_item").prev().find("a").text();
		} else {
			var prev = "<li class='prev'>" + $("ul.other li:last a").parent().html() + "</li>";
			var prev_title = $("ul.other li:last a").text();
		}
		if($("ul.other li.current_page_item").next().html() != null) {
			var next = "<li class='next'>" + $("ul.other li.current_page_item").next().html() + "</li>";
			var next_title = $("ul.other li.current_page_item").next().find("a").text();
		} else {
			var next = "<li class='next'>" + $("ul.other li:first a").parent().html() + "</li>";
			var next_title = $("ul.other li:first a").text();
		}
		$("p.navigation").before('<ul class="controls">' + prev + next + '</ul>');
		
		$("li.prev a").attr("id", "prev");
		$("li.prev a").attr("title", prev_title);
		$("li.next a").attr("id", "next");
		$("li.next a").attr("title", next_title);
		
		document.onkeydown = NavigateThrough;
		
		$("div.placeholder p img").wrap('<a href="'+$("#next").attr("href")+'"></a>')
	}
	externalLinks();
	
	if($("form.contact").length) {
		jQuery(function() {
			var loader = jQuery('<div id="loader"><img src="http://posterhouse.org/wp-content/themes/posterhouse/img/loader.gif" alt="зареждане…" /></div>')
				.css({position: "relative", top: "0", left: "0"})
				.appendTo("form.contact")
				.hide();
			jQuery().ajaxStart(function() {
				loader.show();
				$("form.contact p.button input").attr("disabled", "disabled");
			}).ajaxStop(function() {
				loader.hide();
				$("form.contact p.button input").removeAttr("disabled");
			}).ajaxError(function(a, b, e) {
				throw e;
			});
			
			if(lang == "bg") {
				$("form.contact").validate({
					messages: {
				     email: {
				       required: "Моля, въведете вашата електронна поща.",
				       email: "Моля, въведете валидна електронна поща във формата name@domain.com"
				     }
				   },
				   	submitHandler: function(form) {
						jQuery(form).ajaxSubmit({
							target: "#result",
							success: showResponse
						});
					}
				});
			} else if(lang == "en") {
				$("form.contact").validate({
					messages: {
				     email: {
				       required: "This field is required.",
				       email: "Please enter a valid email address"
				     }
				   },
				   	submitHandler: function(form) {
						jQuery(form).ajaxSubmit({
							target: "#result",
							success: showResponse
						});
					}
				});
			}
			
		});
	}
});

function NavigateThrough (event) {
    if (!document.getElementById) return;
    
    if (window.event) event = window.event;
    
    //if (event.ctrlKey) {
        var link = null;
        var href = null;
        
        switch (event.keyCode ? event.keyCode : event.which ? event.which : null) {
        
            case 37:
                link = document.getElementById("prev");
                break;
            case 39:
                link = document.getElementById("next");
                break;
        }
        
        if (link && link.href) document.location = link.href;
        if (href) document.location = href;
    //}
}

function externalLinks() {
	$("a[rel=external]").attr("target", "_blank");
}

function showResponse(){ 
	jQuery("form.contact").resetForm();
}