jQuery.cookie=function(b,j,m){if(typeof j!="undefined"){m=m||{};if(j===null){j="";m.expires=-1}var e="";if(m.expires&&(typeof m.expires=="number"||m.expires.toUTCString)){var f;if(typeof m.expires=="number"){f=new Date();f.setTime(f.getTime()+(m.expires*24*60*60*1000))}else{f=m.expires}e="; expires="+f.toUTCString()}var l=m.path?"; path="+(m.path):"";var g=m.domain?"; domain="+(m.domain):"";var a=m.secure?"; secure":"";document.cookie=[b,"=",encodeURIComponent(j),e,l,g,a].join("")}else{var d=null;if(document.cookie&&document.cookie!=""){var k=document.cookie.split(";");for(var h=0;h<k.length;h++){var c=jQuery.trim(k[h]);if(c.substring(0,b.length+1)==(b+"=")){d=decodeURIComponent(c.substring(b.length+1));break}}}return d}};

function get_weather(city) {
 		 $("#loading").show();
 		 $.ajax({
			  	type: "GET",
   				url: "/pogoda/",
   				data: "city=" + city,
   				success: function(data) {
     				$("#result").html(data);
     				$("h2.locations").html($("#loc",data).html());     				
     			},
     			complete: function () {
					$("#loading").hide();
				}
         });
}

function do_user_rate(uid,dir,post) {
 		 $("#loading_small_"+post).show();
 		 $.ajax({
			  	type: "GET",
   				url: "/index.php",
   				data: "a=do_user_rate&m=profile&dir=" + dir + "&u=" + uid,
   				success: function(data) {
     				$("span.usr_rtng_"+uid).html(data);
     			},
     			complete: function () {
     				$("#loading_small_"+post).hide();
     			}
         });
}

function initSpoilers(context) {
    var context = context || 'body';
    $('div.sp-head', $(context))
        .click(function(){
            $(this).toggleClass('unfolded');
            $(this).next('div.sp-body').slideToggle('fast');
        })
    ;
}

function initSmiles() {
    $('div.sp-head', $('#smiles'))
        .click(function(){
            $(this).toggleClass('folded');
            $(this).next('div.sp-body').slideToggle('fast');
        })
    ;
}

function getWindowWidth () {
    return ((document.documentElement) ? document.documentElement.clientWidth : document.body.clientWidth) - 20
}

var wWidth = getWindowWidth();
if(wWidth<1024) var postImg_MaxWidth = wWidth - 300;
else var postImg_MaxWidth = 700;

function imgFit_Onload (context) {
    var context = context || 'body';
    var $img = $('img.postImg', $(context));
    if ($.browser.msie || context == 'body') {
        $img.each(function(){ imgFit(this); });
    } else {
        $img.load(function(){ imgFit(this); });
    }
    $img.click(function(){ return imgFit(this); });
}

function imgFit (img, maxW) {
    if (typeof(img.naturalWidth) == 'undefined') {
        img.naturalHeight = img.height;
        img.naturalWidth  = img.width;
    }
    if (!maxW) {
        maxW = postImg_MaxWidth;
    }
    
    if (img.width > maxW) {
        img.height = Math.round((maxW/img.width)*img.height);
        img.width  = maxW;
        img.title  = 'Кликните по картинке для просмотра в оригинальном размере';
        img.style.cursor = 'move';
        return false;
    }
    else if (img.width == maxW && img.width < img.naturalWidth) {
        img.height = img.naturalHeight;
        img.width  = img.naturalWidth;
        img.title  = 'Кликните, чтобы вписать картинку в размеры окна';
        return false;
    }

    return true;
}

function initKey(event) {
    if (window.event) event = window.event;
    if (event.ctrlKey) {
        var link = null;
        switch (event.keyCode ? event.keyCode : event.which ? event.which : null) {
            case 0x25:
                link = $('#PrevLink').attr('href');
                break;
            case 0x27:
                link = $('#NextLink').attr('href');
                break;
        }
        if (link) window.location = link;
    }    
}

$(document).ready(function(){
    initSpoilers('body');
	imgFit_Onload('body');
    $(this).keydown(initKey);
	$("a.fancyimg").fancybox({
		'type': 'image',
		'titlePosition'  : 'inside'
	});
});

document.documentElement.id = "js";

