$(document).ready(function() {	
	// Yaroslavl Scenery
	$(".background").width(function() {
		var width = $(window).width();
		var height = $(window).height();
		if(width<height) {
			$(".background").css("height","100%");
			$(".background").css("width","auto");
		}
	});
	
	// Preload Images
	jQuery.preLoadImages("http://static.yarfor.me/img/navigation_c.png");
	
	// Yandex Key
	key = "AFpgn0sBAAAAIr1SewIA8rbJJzb1qc7f3b0-hYSleZjzS2AAAAAAAAAAAAASaBr6aJljHOA_JPC-b-tJ7FqNNw==";
	
	// Promo
	$(".promo .one").addClass(promo.one["class"]);
	$(".promo .two").addClass(promo.two["class"]);
	$(".promo .three").addClass(promo.three["class"]);
	
	$(".promo .one").find("img").attr("src", promo.one["image_url"]);
	$(".promo .two").find("img").attr("src", promo.two["image_url"]);
	$(".promo .three").find("img").attr("src", promo.three["image_url"]);
	
	$(".promo .one").find("a").html(promo.one["title"]);
	$(".promo .two").find("a").html(promo.two["title"]);
	$(".promo .three").find("a").html(promo.three["title"]);
	
	$(".promo .one").find("a").attr("href", promo.one["url"]);
	$(".promo .two").find("a").attr("href", promo.two["url"]);
	$(".promo .three").find("a").attr("href", promo.three["url"]);
	
	$(".promo .spotlight").fadeIn("slow");
	
	$(".promo li").click(function() {
		window.location = $(this).find("a").attr("href");
	});
	
	// Fancybox
	$(".post a").attr('href', function() {
		var url = this.href;
		var baseURL;
		
		if(url.indexOf("?")!==-1){ //ff
			baseURL = url.substr(0, url.indexOf("?"));
		}else{ 
			baseURL = url;
		}
		
		var urlString = /\.jpg$|\.jpeg$|\.png$|\.gif$|\.bmp$/;
		var urlType = baseURL.toLowerCase().match(urlString);
	    if(urlType == '.jpg' || urlType == '.jpeg' || urlType == '.png' || urlType == '.gif' || urlType == '.bmp'){
	    	// Add style
	    	$(this).addClass("fancybox");
	    	
	    	// Add "Plus"
	    	$(this).append('<div class="plus"></div>');
	    	
	    	// Add title
	    	$(this).append('<div class="description"><span>' + this.title + '</span></div>');
	    	
	    	// Add Fancybox
	    	$(this).fancybox({
	    		'titlePosition': 'over'
	    	});
	    }
	});
	
    // Remove underline from images
    $("a:has(img)").addClass("imglink");  
	
	// Navigation
	$(".navigation li, .rss").hover(function() {
		var image_url = $(this).css("backgroundImage");
		image_url = image_url.replace('.png','_c.png');
		
		$(this).css("backgroundImage", image_url);
	},
	function() {
		var image_url = $(this).css("backgroundImage");
		image_url = image_url.replace('_c.png','.png');
		
		$(this).css("backgroundImage", image_url);
	});
	$(".navigation li, .rss").click(function(){
		var href = $(this).children("a").attr("href");
		if(href) {
			window.location = href;
		}
	});
	
	
	// Map
	$(".post span.map").val(function() {
		var span = $(this);
		
		showmap(span);
	});
	$(".post span.map").hover(function() {
		if($.browser.msie){
			$(this).children(".mapbox").show();
		} else {
			$(this).children(".mapbox").fadeIn();
		}
	},
	function() {
		if($.browser.msie){
			$(this).children(".mapbox").hide();
		} else {
			$(this).children(".mapbox").fadeOut();
		}
	});
	
	// Comments 
	$("#comments li:even").addClass("even");
	
	// Fix Flash z-index Problem
	$("object").append('<param name="wmode" value="transparent">');
	$("embed").attr("wmode","transparent");
	
	// Hack with tags
	// Nothing (everything in CSS)
});

/**
 * Show Yandex Map 
 * 
 * @param span
 * @return
 */
function showmap(span) {
	// Geocoder
	var geocoder = new YMaps.Geocoder("Ярославль, " + span.attr("title"), {results: 1});
	
    YMaps.Events.observe(geocoder, geocoder.Events.Load, function () {
    	if (this.length()) {
    		var geoResult = this.get(0);
    		coor = geoResult.getGeoPoint().toString();
    		
    		// Add Map
    		span.append('<div class="mapbox"><img src="http://static-maps.yandex.ru/1.x/?ll=' + coor 
        				+ '&pt=' + coor +',pmblm&z=14&l=map&size=230,150&key=' + key + '" /></div>');
    	} else {
    		// Remove underline
    		span.css('border-bottom', 'none');
    	}
     });
}

(function($) {
	var cache = [];
	// Arguments are image paths relative to the current page.
	$.preLoadImages = function() {
		var args_len = arguments.length;
		for (var i = args_len; i--;) {
			var cacheImage = document.createElement('img');
			cacheImage.src = arguments[i];
			cache.push(cacheImage);
		}
	  };
	})(jQuery);
