/**
 * @author Alexander Farkas
 * v. 1.02
**/
(function($) {
	$.extend($.fx.step, {
	    backgroundPosition: function(fx) {
            if (fx.state === 0 && typeof fx.end == 'string') {
                var start = $.curCSS(fx.elem, 'backgroundPosition');
                start = toArray(start);
                fx.start = [start[0], start[2]];
                var end = toArray(fx.end);
                fx.end = [end[0], end[2]];
                fx.unit = [end[1], end[3]];
			}
            var nowPosX = [];
            nowPosX[0] = ((fx.end[0] - fx.start[0]) * fx.pos) + fx.start[0] + fx.unit[0];
            nowPosX[1] = ((fx.end[1] - fx.start[1]) * fx.pos) + fx.start[1] + fx.unit[1];
            fx.elem.style.backgroundPosition = nowPosX[0] + ' ' + nowPosX[1];

           function toArray(strg){
               strg = strg.replace(/left|top/g, '0px');
               strg = strg.replace(/right|bottom/g, '100%');
               strg = strg.replace(/([0-9\.]+)(\s|\)|$)/g, '$1px$2');
               var res = strg.match(/(-?[0-9\.]+)(px|\%|em|pt)\s(-?[0-9\.]+)(px|\%|em|pt)/);
               return [parseFloat(res[1],10), res[2], parseFloat(res[3], 10), res[4]];
           }
        }
	});
})(jQuery);

/*-----------------------
* jQuery Plugin: Scroll to Top
* by Craig Wilson, Ph.Creative (http://www.ph-creative.com)
* 
* Copyright (c) 2009 Ph.Creative Ltd.
* Description: Adds an unobtrusive "Scroll to Top" link to your page with smooth scrolling.
* For usage instructions and version updates to go http://blog.ph-creative.com/post/jquery-plugin-scroll-to-top.aspx
* 
* Version: 1.0, 12/03/2009
-----------------------*/
$(function(){$.fn.scrollToTop=function(){$(this).hide().removeAttr("href");if($(window).scrollTop()!="0"){$(this).fadeIn("slow")}var scrollDiv=$(this);$(window).scroll(function(){if($(window).scrollTop()=="0"){$(scrollDiv).fadeOut("slow")}else{$(scrollDiv).fadeIn("slow")}});$(this).click(function(){$("html, body").animate({scrollTop:0},"slow")})}});

$(function() {
	// Main Menu
	$("#menu a.animated")
		.css( {backgroundPosition: "0 0"} )
		.mouseover(function(){
			$(this).stop().animate({backgroundPosition : "(-220px 0)"}, {duration: 500})
		})
		.mouseout(function(){
			$(this).stop().animate({backgroundPosition : "(-400px 0)"}, {duration: 200, complete: function(){
				$(this).css({backgroundPosition: "0 0"})
			}})
		});

	// Search Field 
	$("#searchField").bind("focus blur", function() {
		if ( $(this).val() == "Търсене за.." ) { $(this).val(""); }
		$(this).toggleClass("activeSearchField");
	});
	$("#searchField").bind("blur", function() {
		if ( $(this).val() == "" ) { $(this).val("Търсене за.."); }
	});

	// Header Slideshow - Special Advertising Formats
	var campaign = true;
	if (campaign) {
		var setNavigationStyle = function(index) {
			$("#navigationDiv .trigger").css({"background" : "#F4F4F4", "-moz-border-radius" : "6px", "-webkit-border-radius" : "6px"});
			$("#navigationDiv .trigger p").css({"color" : "#999999"});
			$("#navigationDiv #t_" + index).css({"background" : "#FFFFFF"});
			$("#navigationDiv #t_" + index + " p").css({"color" : "#666666"});
		}		
		// Init slides and navigation
		var i = 1;
		$("#mainFrame .slides").each(function() { $(this).attr("id", "s_" + i);	i++; });
		var i = 1;
		$("#navigationDiv .trigger").each(function() { $(this).attr("id", "t_" + i); i++; });
		setNavigationStyle(1);		
		var ss = true;
		var ss_init = true;
		var d = 3000; // 3 seconds
		var total = 3;
		var c = 1;
		var last = 0;
		var timers = new Array();		
		// Hover Slide Show - v. 0.1 24.11.2009, Vladimir Komarov
		if (ss) {
			var makeSlideShow = function() {
				if (!ss_init) {							
					// Counter
					c++; if (last) { last >= total ? c = 1 :  c = last + 1; last = 0; }
					// Change slide
					$("#mainFrame .slides").hide();
					$("#mainFrame #s_" + c).fadeIn("slow");						
					setNavigationStyle(c);							
					// Reset counter
					if (c >= total) { c = 0; }
				}
				else if (ss_init) { ss_init = false; }
				// Repeat after X seconds (var d)
				timers.push( setTimeout(makeSlideShow, d) );
			}
			makeSlideShow();
		}		
		// Handle Events
		// Hover - Triggers
		$("#navigationDiv .trigger").hover(
			function() {
				// Get ID
				var ID = $(this).attr("id");
				var index = parseInt(/\d/.exec(ID));				
				// Stop slideshow - Reset slide show setTimeout's
				for (var t = 0; t <= timers.length; t++) { clearTimeout(timers[t]); }
				// First we stop slideshow, then start it again with new index
				last = index;
				// Change slide
				$("#mainFrame .slides").hide();
				$("#mainFrame #s_" + index).fadeIn("slow");				
				setNavigationStyle(index);
			},
			function() {
				// Start slideshow again
				ss_init = true;
				makeSlideShow();
			}
		);
		// Hover - Slides
		$("#mainFrame .slides").hover(
			function() {
				// Get ID
				var ID = $(this).attr("id");
				var index = parseInt(/\d/.exec(ID));
				// Stop slideshow - Reset slide show setTimeout's
				for (var t = 0; t <= timers.length; t++) { clearTimeout(timers[t]); }
				// First we stop slideshow, then start it again with new index
				last = index;
			},
			function() {
				// Start slideshow again
				ss_init = true;
				makeSlideShow();
			}
		);
	}
	// End slideshow

	// Left Sidebar	
	$("#social").load(root + 'ajax/get-social.php', '', function () {	
		$("#social").fadeIn(1500);					
	});	
	
	// Right Sidebar
	var randomNumber = 1000000;
	var data = '';	
	$("#earth").bind("click", function() {
		randomNumber = Math.floor(Math.random() * 1000000);
		$("#dailyInfo").slideUp("slow", function() {	
			$("#earth").hide();
			$("#dailyAjaxLoader").show();
			$("#dailyInfo").load(root + 'ajax/get-daily-info.php?' + randomNumber, data, function() {
				$("#dailyAjaxLoader").hide();
				$("#dailyInfo").fadeIn(3000);					
			});			
		});		
	});

	// Google Analytics
	$("a[class*='external']").bind("click", function(){
		pageTracker._trackPageview('/outgoing/'+ $(this).attr('href'));
	});

	// Misc
	var extImg = ' <img class="extLinkImg" src="' + root + 'images/icons/i-external.png" alt="External Link" />';
	$("a.external").each(function() { $(this).append(extImg); });	
	var photoImg = ' <img class="photoLinkImg" src="' + root + 'images/icons/i-photo.png" alt="Photo" style="border: 0; margin: 0 3px; padding: 0;" />';
	$("a.photo").each(function() { $(this).prepend(photoImg); });
	// Localhost Images Path Fix
	if (strstr(root, "localhost")) {
		var postImages = $("div#pageContent img[src^='/files/']");
		postImages.each(function() {
			$(this).attr("src", root + $(this).attr("src")); 
		});
	}	

	$("#enterBttn").hover(
		function() { $(this).attr("src", root + "images/bttns/bttn-enter-a.jpg"); },
		function() { $(this).attr("src", root + "images/bttns/bttn-enter.jpg");	}
	);

	$("#registrationBttn").hover(
		function() { $(this).attr("src", root + "images/bttns/bttn-registration-a.jpg"); },
		function() { $(this).attr("src", root + "images/bttns/bttn-registration.jpg"); }
	);

	// Footer Stats
	$("#footerStats").load(root + 'ajax/get-footer-stats.php', {}, function() {
		$("a#gravatarLink").hover(
			function (e) {
				$(this).attr("href", "http://en.gravatar.com/");
				$("div#borders").css({"-moz-border-radius" : "10px", "-webkit-border-radius" : "10px"});
				$("div#footerGravatarInvContainer img").css({"-moz-border-radius" : "10px", "-webkit-border-radius" : "10px"});
				$("div#footerGravatarInvContainer").css({"left" : e.pageX - 10 + "px", "top" : e.pageY - 125 + "px", "opacity" : "0.9",  "-moz-border-radius" : "5px", "-webkit-border-radius" : "5px"}).fadeIn(700);
			},
			function (e) {
				$(this).attr("href", "#");
				$("div#footerGravatarInvContainer").fadeOut(200);
			}
		);
	});

	/*****************************************/
	// Comments
	var commentTxtArea		= $("#commentContent");
	var commentSubmitBttn	= $("#commentSubmitBttn");
	var jsAutogrow = root + 'js/jquery.autogrow.js';
	var jsCyrillic = root + 'js/jquery.cyrillic.0.9.js';	

	commentTxtArea.bind("mouseover", function() {
		$("#comments").append("<script src=\"" +  jsAutogrow + "\" type=\"text/javascript\"></script>");
		$("#comments").append("<script src=\"" +  jsCyrillic + "\" type=\"text/javascript\"></script>");
		if (commentTxtArea.val() === 'Вашият коментар тук..') { commentTxtArea.empty(); }
		commentTxtArea.unbind("mouseover").autogrow().cyrillic().focus();
	});

	// Events

	$("#cc").bind("click", function(e) {
		e.preventDefault();
		return false;
	});

	// Gravatar Mouseover Effect / CSS 3 Stuffs
	$(".gravatar").bind("mouseover", function(e) {
		var cmID = $(e.target).closest("div.comment").attr("id");
		var link = $(this).parent().attr("href");
		var modifiedURL = $(this).attr("src").replace('40x40.png&size=40', '80x80.png&size=80');
		var preview = '<div class="gravatarBig ' + cmID + '"><a href="' + link + '" title="Направете си Gravatar" target="_blank"><img src="' + modifiedURL + '" border="0" alt="Loading.." /></a></div>';
		$(e.target).closest("div.userInfo").append(preview);
		$("div." + cmID)
			.css({"-moz-border-radius" : "7px", "-webkit-border-radius" : "7px",
				"-moz-box-shadow" : "3px 3px 3px #E4E4E4", "-webkit-box-shadow" : "3px 3px 3px #E4E4E4",
				"filter" : "progid:DXImageTransform.Microsoft.dropShadow(color=#E4E4E4,offX=3,offY=3,positive=true)"})
			.fadeIn(700).children().attr("alt", "Gravatar")
			.bind("mouseout", function() {
				$(this).removeClass("gravatarBig").fadeOut(150);
				$(".gravatarBig").remove();
			});		
	});

	$("#toTop").scrollToTop();

	// Comments CSS 3 Rules
	$("#lang").css({
		"-moz-border-radius" : "3px", 
		"-webkit-border-radius" : "3px",
		"box-shadow" : "3px 3px 5px #E4E4E4",
		"-moz-box-shadow" : "3px 3px 5px #E4E4E4",
		"-webkit-box-shadow" : "3px 3px 5px #E4E4E4",
		"filter" : "progid:DXImageTransform.Microsoft.dropShadow(color=#E4E4E4,offX=3,offY=3,positive=true)"
	});	
	commentTxtArea.css({
		"-moz-border-radius" : "3px", 
		"-webkit-border-radius" : "3px",
		"box-shadow" : "3px 3px 5px #E4E4E4",
		"-moz-box-shadow" : "3px 3px 5px #E4E4E4",
		"-webkit-box-shadow" : "3px 3px 5px #E4E4E4",
		"filter" : "progid:DXImageTransform.Microsoft.dropShadow(color=#E4E4E4,offX=3,offY=3,positive=true)",
		"min-height" : "100px"
	});
	commentSubmitBttn.css({
		"-moz-border-radius" : "2px", 
		"-webkit-border-radius" : "2px",
		"box-shadow" : "3px 3px 5px #E4E4E4",
		"-moz-box-shadow" : "3px 3px 5px #E4E4E4",
		"-webkit-box-shadow" : "3px 3px 5px #E4E4E4",
		"filter" : "progid:DXImageTransform.Microsoft.dropShadow(color=#E4E4E4,offX=3,offY=3,positive=true)"
	});
	$(".gravatar").css({"-moz-border-radius" : "3px", "margin" : "0 5px 0 0"});

	$(".grey").css({
		"-moz-border-radius-topright" : "20px",
		"-moz-border-radius-bottomleft" : "10px", 
		"-webkit-border-top-right-radius" : "20px",
		"-webkit-border-bottom-left-radius" : "10px",
		"-moz-box-shadow" : "1px 1px 5px #E4E4E4",
		"-webkit-box-shadow" : "1px 1px 5px #E4E4E4",
		"filter" : "progid:DXImageTransform.Microsoft.dropShadow(color=#E4E4E4,offX=1,offY=1,positive=true)"
	});

	// Comments End
	/*****************************************/
});

// Functions
function strstr (haystack, needle) { 
    var pos = 0;    
    haystack += '';
    pos = haystack.indexOf( needle );
    if (pos == -1) { return false; }
	else { return haystack.slice( pos ); }
}