$(document).ready(function() {
    $('#events-carousel').jcarousel({
        vertical: true,
        scroll: 1,
        wrap: 'circular',
        itemVisibleInCallback: {
		  onBeforeAnimation: re_render
//		  onAfterAnimation: re_render
		}
    });
    
    $('.big-img').click(function(){
    	$('.loading').show();
    	$('.big-img .active-image').remove();
    	$(this).prepend('<div class="active-image"></a>');
    	$('#main-image').attr('src',$(this).attr('rel'));
    	$('#main-image').load(function(){
    		$('.loading').hide();
    	});
    	return false;
    });
    
    $(".only-admin").animateOnlyAdmin();
    
    $('#region').click(function(){
    	if ($('#region-menu').is(':visible')){
    		$('#region-menu').hide("blind", { direction: "vertical" }, 200);
    	}else{
    		$('#region-menu').show("blind", { direction: "vertical" }, 200);
    	}
    });
    
    $('#region-menu a').click(function(){
    	var region = $(this).attr('rel');
    	$('#region').html($(this).html());
    	$('#region-menu').hide("blind", { direction: "vertical" }, 200);
    	$('#region-menu a').removeClass('act');
    	$(this).addClass('act')
    	
    	$.get('/?region_change=' + region,function(){location.reload();});
		
    });
    
    $('#query').keyup(function(){
    	$('#search-button').hide();
    	$('#search-loading').show();
    	setTimeout("$('#search-button').show(); $('#search-loading').hide();if($('#query').val().length >= 2){$('#search-results').show('blind', { direction: 'vertical' }, 200);}else{if($('#search-results').is(':visible')){$('#search-results').hide('blind', { direction: 'vertical' }, 200);}}",1000);
    });
    
    
    var mouse_is_inside = false;

    $('#region-menu').hover(function(){ 
        mouse_is_inside=true; 
    }, function(){ 
        mouse_is_inside=false; 
    });

    $('body').mouseup(function(){ 
        if(! mouse_is_inside) $('#region-menu').hide();
    });
    
    var mouse_is_inside_search = false;

    $('#search-results').hover(function(){ 
        mouse_is_inside_search=true; 
    }, function(){ 
        mouse_is_inside_search=false; 
    });

    $('body').mouseup(function(){ 
        if(! mouse_is_inside_search) $('#search-results').hide();
    });


    
    var posts_one = $('.posts-column-one > .post-body');
    var posts_two = $('.posts-column-two > .post-body');
    var posts_three = $('.posts-column-three > .post-body');
    var curr_one=0, node_one;
    var curr_two=0, node_two;
    var curr_three=0, node_three;

    $('#scroll-down').click(function(){
    	
		node_one = posts_one[++curr_one];
		if (node_one) {
			$('.posts-column-one').scrollTo( node_one,randomXToY(150,800));
		}else{
			curr_one = 0;
			node_one = posts_one[curr_one];
			$('.posts-column-one').scrollTo( node_one,randomXToY(150,800));
		}
		
		node_two = posts_two[++curr_two];
		if (node_two) {
			$('.posts-column-two').scrollTo( node_two,randomXToY(150,800));
		}else{
			curr_two = 0;
			node_two = posts_two[curr_two];
			$('.posts-column-two').scrollTo( node_two,randomXToY(150,800));
		}
		
		node_three = posts_three[++curr_three];
		if (node_three) {
			$('.posts-column-three').scrollTo( node_three,randomXToY(150,800));
		}else{
			curr_three = 0;
			node_three = posts_three[curr_three];
			$('.posts-column-three').scrollTo( node_three,randomXToY(150,800));
		}
          
    });
    
    $('#scroll-up').click(function(){
    	
		node_one = posts_one[--curr_one];
		if (node_one) {
			$('.posts-column-one').scrollTo( node_one,randomXToY(150,800));
		}else{
			curr_one = posts_one.length-1;
			node_one = posts_one[curr_one];
			$('.posts-column-one').scrollTo( node_one,randomXToY(150,800));
		}
		
		node_two = posts_two[--curr_two];
		if (node_two) {
			$('.posts-column-two').scrollTo( node_two,randomXToY(150,800));
		}else{
			curr_two = posts_two.length-1;
			node_two = posts_two[curr_two];
			$('.posts-column-two').scrollTo( node_two,randomXToY(150,800));
		}
		
		node_three = posts_three[--curr_three];
		if (node_three) {
			$('.posts-column-three').scrollTo( node_three,randomXToY(150,800));
		}else{
			curr_three = posts_three.length-1;
			node_three = posts_three[curr_three];
			$('.posts-column-three').scrollTo( node_three,randomXToY(150,800));
		}
          
    });
    
    setTimeout("$('.error').addClass('expired');",5000);
    $('.ad-preview').click(function(){ return false; });
    $('.ad-preview').hover(function(){
    	var href = $(this).attr('href');
    	var src = $(this).attr('rel');
    	$('#main-image-href').attr('href',href);
    	$('#main-image-src').attr('src',src);
    });
});

function re_render(){
	Cufon.replace('.day,.month,.time,.title');
}

function randomXToY(minVal,maxVal,floatVal)
{
  var randVal = minVal+(Math.random()*(maxVal-minVal));
  return typeof floatVal=='undefined'?Math.round(randVal):randVal.toFixed(floatVal);
}

(function(d){d.each(["backgroundColor","borderBottomColor","borderLeftColor","borderRightColor","borderTopColor","color","outlineColor"],function(f,e){d.fx.step[e]=function(g){if(!g.colorInit){g.start=c(g.elem,e);g.end=b(g.end);g.colorInit=true}g.elem.style[e]="rgb("+[Math.max(Math.min(parseInt((g.pos*(g.end[0]-g.start[0]))+g.start[0]),255),0),Math.max(Math.min(parseInt((g.pos*(g.end[1]-g.start[1]))+g.start[1]),255),0),Math.max(Math.min(parseInt((g.pos*(g.end[2]-g.start[2]))+g.start[2]),255),0)].join(",")+")"}});function b(f){var e;if(f&&f.constructor==Array&&f.length==3){return f}if(e=/rgb\(\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*\)/.exec(f)){return[parseInt(e[1]),parseInt(e[2]),parseInt(e[3])]}if(e=/rgb\(\s*([0-9]+(?:\.[0-9]+)?)\%\s*,\s*([0-9]+(?:\.[0-9]+)?)\%\s*,\s*([0-9]+(?:\.[0-9]+)?)\%\s*\)/.exec(f)){return[parseFloat(e[1])*2.55,parseFloat(e[2])*2.55,parseFloat(e[3])*2.55]}if(e=/#([a-fA-F0-9]{2})([a-fA-F0-9]{2})([a-fA-F0-9]{2})/.exec(f)){return[parseInt(e[1],16),parseInt(e[2],16),parseInt(e[3],16)]}if(e=/#([a-fA-F0-9])([a-fA-F0-9])([a-fA-F0-9])/.exec(f)){return[parseInt(e[1]+e[1],16),parseInt(e[2]+e[2],16),parseInt(e[3]+e[3],16)]}if(e=/rgba\(0, 0, 0, 0\)/.exec(f)){return a.transparent}return a[d.trim(f).toLowerCase()]}function c(g,e){var f;do{f=d.curCSS(g,e);if(f!=""&&f!="transparent"||d.nodeName(g,"body")){break}e="backgroundColor"}while(g=g.parentNode);return b(f)}var a={aqua:[0,255,255],azure:[240,255,255],beige:[245,245,220],black:[0,0,0],blue:[0,0,255],brown:[165,42,42],cyan:[0,255,255],darkblue:[0,0,139],darkcyan:[0,139,139],darkgrey:[169,169,169],darkgreen:[0,100,0],darkkhaki:[189,183,107],darkmagenta:[139,0,139],darkolivegreen:[85,107,47],darkorange:[255,140,0],darkorchid:[153,50,204],darkred:[139,0,0],darksalmon:[233,150,122],darkviolet:[148,0,211],fuchsia:[255,0,255],gold:[255,215,0],green:[0,128,0],indigo:[75,0,130],khaki:[240,230,140],lightblue:[173,216,230],lightcyan:[224,255,255],lightgreen:[144,238,144],lightgrey:[211,211,211],lightpink:[255,182,193],lightyellow:[255,255,224],lime:[0,255,0],magenta:[255,0,255],maroon:[128,0,0],navy:[0,0,128],olive:[128,128,0],orange:[255,165,0],pink:[255,192,203],purple:[128,0,128],violet:[128,0,128],red:[255,0,0],silver:[192,192,192],white:[255,255,255],yellow:[255,255,0],transparent:[255,255,255]}})(jQuery);

jQuery.fn.animateOnlyAdmin = function() {
	this
		.animate({ backgroundColor: "#ffccd4" }, 1000)
		.animate({ backgroundColor: "#fff" }, 1000, function() {
			$(this).animateOnlyAdmin();
		});
	return this;
};
