jQuery(function($){
	/**
	  * News rotate
	  */
	var post_id,post_title,post_description,post_attributes, rotateTimeOutRef1, rotateTimeOutRef2, rotateTimeOutRef3;
	if(zero_bnr == 0) {
		var curid = 1;
	} else {
		var curid = 0;
	}
	$('.center_block_menu li').mouseenter(function(){
		if(zero_bnr) {
			$('#index_block_bnr').css('display','block');
			$('#index_post_big_image').css('display','block');
			$('#zero_bnr_holder').css('display','none');
		}
		post_id = $(this).attr('data-post-id');
		post_img_src = $(this).attr('data-post-big-image');
		post_title = $('#post_title_'+post_id).html();
		post_href = $('#post_href_'+post_id).html();
		if(post_title.length > 47) {
			post_title = post_title.substr(0,44) + '...';
		}
		post_description = $('#post_description_'+post_id).html();
		post_attributes = $('#post_attributes_'+post_id).html();
		$('.center_block_menu li').removeClass('act');
		$(this).addClass('act');
		$('#index_header').html(post_title);
		$('#index_href_value').attr('href',post_href);
		if (post_href.indexOf('mjoy.com.ua') < 0 && post_href.indexOf('http') != -1) $('#index_href_value').attr('target','_blank');
		else $('#index_href_value').attr('target','_self');
		$('#index_description').html(post_description);
		$('#index_post_attributes').html(post_attributes);
		$('#index_post_big_image').attr("src",post_img_src).show();
		//$('#index_post_big_image').fadeOut().attr("src",post_img_src).show();
		/*
		$("#index_post_big_image").fadeOut(function() {
			$(this).load(function() { $(this).fadeIn(); });
			$(this).attr("src", post_img_src);
		});
		*/
		clearTimeout(rotateTimeOutRef1);
		clearTimeout(rotateTimeOutRef2);
		clearTimeout(rotateTimeOutRef3);
	}).mouseleave(function(){
		rotateTimeOutRef3 = setTimeout(rotateIndexNews, 20000);
	});
	function rotateIndexNews() {
		if(zero_bnr) {
			$('#index_block_bnr').css('display','block');
			$('#index_post_big_image').css('display','block');
			$('#zero_bnr_holder').css('display','none');
		}
		post_id = $('.center_block_menu li').eq(curid).attr('data-post-id');
		post_img_src = $('.center_block_menu li').eq(curid).attr('data-post-big-image');
		post_title = $('#post_title_'+post_id).html();
		post_href = $('#post_href_'+post_id).html();
		if(post_title.length > 47) {
			post_title = post_title.substr(0,44) + '...';
		}
		post_description = $('#post_description_'+post_id).html();
		post_attributes = $('#post_attributes_'+post_id).html();
		$('.center_block_menu li').removeClass('act');
		$('.center_block_menu li').eq(curid).addClass('act');
		$('#index_header').html(post_title);
		$('#index_href_value').attr('href',post_href);
		$('#index_description').html(post_description);
		$('#index_post_attributes').html(post_attributes);
		$("#index_post_big_image").fadeOut(function() {
			$(this).load(function() { $(this).fadeIn(100); });
			$(this).attr("src", post_img_src);
		});
		curid++;
		if(curid == $('.center_block_menu li').size()) curid = 0;
		rotateTimeOutRef1 = setTimeout(rotateIndexNews, 8000);
	}
	if(page_is_index) {
		if(zero_bnr == 0) {
			rotateTimeOutRef2 = setTimeout(rotateIndexNews, 5000);
		} else {
			rotateTimeOutRef2 = setTimeout(rotateIndexNews, 10000);
		}
	}
	/**
             * Menu dropdown functionality
	  */
	$('.menu ul li').hover(
			function() { $('ul', this).css('display', 'block'); },
			function() { $('ul', this).css('display', 'none'); });

	/**
	  * Twitter & facebook holder
	  */
	$('#tab_facebook').click(function(){
		$('#tab_facebook_class').removeClass('twitter_b2');
		$('#tab_facebook_class').addClass('twitter_b1');
		$('#tab_twitter_class').removeClass('twitter_b1');
		$('#tab_twitter_class').addClass('twitter_b2');
		$('#twitter_holder').hide();
		$('#facebook_holder').show();
		return false;
	});
	$('#tab_twitter').click(function(){
		$('#tab_facebook_class').removeClass('twitter_b1');
		$('#tab_facebook_class').addClass('twitter_b2');
		$('#tab_twitter_class').removeClass('twitter_b2');
		$('#tab_twitter_class').addClass('twitter_b1');
		$('#twitter_holder').show();
		$('#facebook_holder').hide();
		return false;
	});
	/**
	  * Tweets
	  */
	var tweet_query  = "@MjoyRadio+OR+to%3AMjoyRadio+OR+from%3AMjoyRadio";
	tweet_query += "+OR+@radiozkryjivky+OR+to%3Aradiozkryjivky+OR+from%3Aradiozkryjivky";
	tweet_query += "+OR+mjoy.com.ua+OR+onlineshopm.com";
	var tweet_count = 20;
	var tweet_url = 'http://search.twitter.com/search.json?q=from:'+tweet_query+'&rpp='+tweet_count+'&callback=?';

    function relative_time(time_value) {
      var parsed_date = Date.parse(time_value);
      var relative_to = (arguments.length > 1) ? arguments[1] : new Date();
      var delta = parseInt((relative_to.getTime() - parsed_date) / 1000);
      if(delta < 60) {
      return 'меше хвилини тому';
      } else if(delta < 120) {
      return 'біля хвилини тому';
      } else if(delta < (45*60)) {
      return (parseInt(delta / 60)).toString() + ' хвилин(и) тому';
      } else if(delta < (90*60)) {
      return 'біля години тому';
      } else if(delta < (24*60*60)) {
      return 'біля ' + (parseInt(delta / 3600)).toString() + ' годин(и) тому';
      } else if(delta < (48*60*60)) {
      return '1 день тому';
      } else {
      return (parseInt(delta / 86400)).toString() + ' дні(в) тому';
      }
    }

    function linkUrl(string) {
       var regexp = /((ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?)/gi;
       string = string.replace(regexp,"<a href=\"$1\" target=\"_blank\">$1</a>");

       var regexp = /[\@]+([A-Za-z0-9-_]+)/gi;
       string.replace(regexp,"<a href=\"http://twitter.com/$1\" target=\"_blank\">@$1</a>");

       string.replace(/[&lt;]+[3]/gi, "<tt class='heart'>&#x2665;</tt>");

       return string;
    }

	function randOrd(){
		return (Math.round(Math.random())-0.5);
	}
	/*
	var tweet_url = /((ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?)/gi;
	var tweet_url_rpl = "<a href=\"$1\">$1</a>";
	var tweet_usr = /[\@]+([A-Za-z0-9-_]+)/gi;
	var tweet_usr_rpl = "<a href=\"http://twitter.com/$1\">@$1</a>";
	var cur_tweet_msg;
	*/
	$.getJSON(tweet_url, function(data){
		$('#twitter_field').html('');
		$.each(data.results, function(i,item){
			if(i < 10) {
				/*
				cur_tweet_msg = item.text;
				cur_tweet_msg = cur_tweet_msg.replace(tweet_url,tweet_url_rpl);
				cur_tweet_msg = cur_tweet_msg.replace(tweet_usr, tweet_usr_rpl);
				*/
				$('#twitter_field').append('<div class="twitter_message"><div class="grid_1  alpha omega "><a target="blank" href="http://twitter.com/'+ item.from_user+'"><img class="left10 top10 twitterimg" src="'+item.profile_image_url+'" height="30" width="30" alt="" border="0"/></a></div><div class="grid_7  alpha omega "><p class="left10">'+linkUrl(item.text)+'</p><p class="liteblue left10" >'+relative_time(item.created_at)+'</p></div></div>');
			}
		});
		$('#twitter_field').append('<div class="twitter_message"><a href="http://twitter.com/MjoyRadio" target="blank" class="more">Усі дописи</a></div>');
	});

	$.getJSON(HTTP_SITE_ROOT+'/twitter_followers.php', function(data){
		data.sort(randOrd);
		$('#tweet_logos').html('');
		$.each(data, function(i, item){
			if(i<16) {
				$('#tweet_logos').append('<a target="blank" href="http://twitter.com/'+ item.screen_name+'"><img class="twitter_logo" src="'+item.profile_image_url+'" height="30" width="30" alt="" border="0"/></a>');
			}
		});
	});


	/**
	   Partners holder
	  */
	$('#partners_button').toggle(function(){$('#partners_holder').slideDown('fast');},function(){$('#partners_holder').slideUp('fast');});

	/**
	  * Mtop
	  */

	$('.custom_mtop_star').hover(function(){
		$(this).attr('src', 'images/star.png');
	},function(){
		$(this).attr('src', 'images/greystar.png');
	});
	$('.custom_mtop_star').click(function(){
		$(this).attr('src', 'images/star.png');
		$(this).unbind();
		$.ajax({
		   type: "POST",
		   url: HTTP_SITE_ROOT + "/songvote.php",
		   data: "song_artist=" + $(this).attr('data-song-artist') + "&song_title=" + $(this).attr('data-song-title') + "&song_filename="+$(this).attr('data-song-filename'),
		   success: function(msg){

		   }
		 });
	});
});
