//
function check_logout(text) {
	var regexp = /(\|\*code_red\*\|)/;
	if (regexp.test(text)) {
  	//alert("!code_red!");
		window.location = '/';
		return false;
	}
}

$(document).ready(function() {

	// --------------------
	// LOGOs
	// --------------------

	$("p.ivuworks, div.logo-hover").hover(function() {

			var thumbOver = $(this).find("img").attr("src");
			$(this).find("a").css({'background' : 'url(' + thumbOver + ') no-repeat center bottom'});
			$(this).find("span").stop().fadeTo('normal', 0 , function() {
				$(this).hide() 
			});
		
	}, function() { 
			$(this).find("span").stop().fadeTo('normal', 1).show();
	});
	
	// --------------------
	// NAVIGATION
	// --------------------

	$('#navMenu li').append('<div class="hover"><\/div>');

	$('#navMenu li:not(.active)').hover(function() {
			$(this).children('div').stop().css({ opacity: 1 }).fadeIn();	
	}, function() {
			$(this).children('div').fadeOut();	
	});	

	// --------------------
	// SUB NAVIGATION
	// --------------------


	function getSlidingPosition(position, width){		
			var pos_left = Math.round(position.left);
			var pos_width = Math.round(width);
			var newposition = pos_left - 9 + (pos_width/2);
			return newposition;
	}
	
	if ($('#submenu ul li.active').length > 0) {
		
		var acitveSub = $('#submenu ul li.active');
		var startingPosition = getSlidingPosition(acitveSub.position(), acitveSub.width());
	
		$('#submenu ul')
		.css('backgroundPosition', startingPosition + 'px 30px')
		.children('li')
		.mouseover(function(){
		
			var position = $(this).position();
			var width = $(this).width();
			var goto = getSlidingPosition(position, width);
			$(this).parent('ul').stop().animate({ 'background-position': goto + 'px 30px' });
		
		})
		.end()
		.mouseout(function(){
				$(this).stop().animate({ 'background-position': startingPosition + 'px 30px' });
		});
	}

	// ---------------
	// SLIDER
	// ---------------
	
	$('#featured').tabs({ event: 'mouseover' }).tabs("rotate", 3000, true);
			
	$("div.ui-tabs-panel").hover(function() {  
		$(this).tabs("rotate", 0, true);  
	}, function() {  
		$(this).tabs("rotate", 3000, true);  
	});

	// ---------------
	// CHART
	// ---------------

	$("#chartlink").click(function(){
			$(this).hide().prev('#chartimage').hide().end().next('embed').show();
	});

	// ---------------
	// other HOVERs
	// ---------------

	$('div.jshover').hover(function(){
			$(this).addClass('hovered');
	}, function(){
			$(this).removeClass('hovered');
	}).click(function(e){
			var target = $(e.target);
			if(!target.is(".comments")){
				var newsLink = $(this).find('a.more').attr('href');
				window.location.href = newsLink;
			}
	});
	
	$('div.gallery').hover(function(){
			$(this).addClass('hovered');
	}, function(){
			$(this).removeClass('hovered');
	});
	
	$('div.listen_online').hover(function(){
			$(this).addClass('listenhovered');
	}, function(){
			$(this).removeClass('listenhovered');
	});
	
	var searchDiv = $('div.search_site');
	var searchHasFocus = false, searchHovered = false;
	
	$('div.search_site, input#search').hover(function(){
			searchHovered = 1;
			searchDiv.addClass('searchhovered');
	}, function(){
			searchHovered = 0;
			if(!searchHasFocus) searchDiv.removeClass('searchhovered');
	});

	$('input#search').focus(function() {
			searchHasFocus = 1;
			searchDiv.addClass('searchhovered');
	}).blur(function () {
			searchHasFocus = 0;
			if(!searchHovered) searchDiv.removeClass('searchhovered');
	});
	
	$('table.schedule tr').hover(function(){
			$(this).addClass('tr-hover');
	}, function(){
			$(this).removeClass('tr-hover');
	});
	
	// ---------------
	// Shows Slider
	// ---------------
	
	//To switch directions up/down and left/right just place a "-" in front of the top/left attribute

	//Caption Sliding (Partially Hidden to Visible)
	$('.boxgrid.caption').hover(function(){
		$(".cover", this).stop().animate({top:'90px'},{queue:false,duration:300});
	}, function() {
		$(".cover", this).stop().animate({top:'165px'},{queue:false,duration:200});
	});
	
	
	// schedule module - switch between week days
	$('#submenu.unexisting_class_so_you_cant_get_it ul li a').click(function() {
		$('#submenu ul li').removeClass('active');
		var show_menu =	$(this).attr('id');
		var table 		=	$(this).attr('tid');
		var new_title	= $(this).attr('title');
		$('#'+show_menu).parent().addClass('active');
		$('#schedule_title').html(new_title);
		acitveSub = $('#submenu ul li.active');
		startingPosition = getSlidingPosition(acitveSub.position(), acitveSub.width());
		
		$('div.scheduletbl').hide();
		$("#"+table).fadeIn('slow'); 
		return false;
	})
	
	
	// GRAVATAR
	if ($('#gravatar').length > 0) {
		
		$('#gravatar').click(function() {
			$('#gravatar').hide('slow');
			$.cookie("gravatar", "no", { expires: 370, path: '/'});
		})
		
		if ($.cookie("gravatar") == null || $.cookie("gravatar") == "") {
			$('#gravatar').show();
		}
	}
	
	
	// start poll
	if ($('#poll_content').length > 0) {
		if (allowed == 1) {
			ShowPoll(vote_id, 'show_poll', allowed);
		} else {
			ShowPoll(vote_id, 'show_poll', allowed);
		}
	}
	
	function ShowPoll(vote, action, allowed) {
		$.ajax({
		  type: "POST",
		  async: false,
		  url: "/ajax.php",
			data: "action="+action+"&vote_id="+vote+"&allowed="+allowed,				   	
	   	success: function(ajaxresult){
	   		check_logout(ajaxresult);
	    	//alert(ajaxresult);
	    	//console.log(ajaxresult);
	    	if ($('#my_div').length > 0) {
	    		//$('#my_div').html(ajaxresult);
	    	}
	    	var answer = ajaxresult.split("|!|");
			  var content = answer[2];
			  var message = answer[3];
			  if (answer[1] == 1){ // ok
			  	$("#poll_content").html(content);
			  	//var classs = "success";
			  	//show_message(message, classs);
			  } else { // error
			  	//var classs = "error";
			  	if (message) {
			  		$('#vote_message').html(message);
			  	}
			  	//show_message(message, classs);
			  	return false;
			  }
	   	}
		});	
	}
	
	
	// VOTING
	$('#to_vote li a').live('click',function() {
		if (allowed != 0) {
			if (vote_id != 0) {
				$('#'+vote_id).removeClass('active');
			}
			vote_id = $(this).attr('id');
			$('#'+vote_id).addClass('active');
		}
	})

	
	$('#vote_btn').live('click',function() {
		if (vote_id == 0) {
			$('#vote_message').html('За да гласувате първо изберете песен!');
			return;
		}
		$('#to_vote').hide('slow');
		ShowPoll(vote_id, 'show_form');
		
	});
	

	$('#vote_back').live('click',function() {

		$('#poll_content').html();
		ShowPoll(vote_id, 'show_poll');
		$('#'+vote_id).addClass('active');
		
	});

	$('#vote_confirm').live('click',function() {
	
		$.ajax({
		  type: "POST",
		  async: false,
		  url: "/ajax.php",
			data: "action=vote&email="+$("[name=chart_email]").val()+"&name="+$("[name=chart_name]").val()+
			"&family="+$("[name=chart_family]").val()+
			"&phone="+$("[name=chart_phone]").val()+"&city="+$("[name=chart_location]").val()+"&vote_id="+vote_id,				   	
	   	success: function(ajaxresult){
	   		check_logout(ajaxresult);
	    	//alert(ajaxresult);
	    	if ($('#my_div').length > 0) {
	    		//$('#my_div').html(ajaxresult);
	    	}
	    	var answer = ajaxresult.split("|!|");
			  var content = answer[2];
			  var message = answer[3];
			  if (answer[1] == 1){ // ok
			  	$("#poll_content").html(content);
			  	allowed = 0;
			  	$('#'+vote_id).addClass('active');
			  	//var classs = "success";
			  	//show_message(message, classs);
			  } else { // error
			  	var classs = "error";
			  	$('#vote_message').html(message);
			  	$('#vote_message').show('slow');
			  	return false;
			  }
	   	}
		});
		
	});
	
});


function Prepare_Comments (module_id, post_id,l) {
		$.ajax({
		  type: "POST",
		  async: false,
		  url: "/ajax.php",
			data: "action=get_comments&page="+l+"&module_id="+module_id+"&post_id="+post_id,				   	
	   	success: function(ajaxresult){
	   		check_logout(ajaxresult);
	    	//alert(ajaxresult);
	    	if ($('#my_div').length > 0) {
	    		$('#my_div').html(ajaxresult);
	    	}
	    	var answer = ajaxresult.split("|!|");
			  var content = answer[2];
			  var message = answer[3];
			  if (answer[1] == 1){ // ok
			  	$("#comments").html(content);
			  	if (message.length > 0) {
			  		$("#comm_message").html(message);
			  		$("#comm_message").show();
			  	} else {
			  		$("#comm_message").hide();
			  	}
			  	
			  } else { // error
			  	//$("#comments").html('');
			  	if (message.length > 0) {
			  		$("#comm_message").html(message);
			  		$("#comm_message").show();
			  	} else {
			  		$("#comm_message").hide();
			  	}
			  	return false;
			  }
	   	}
		});
	}


	function Add_Comment(module_id, post_id) {
		var datata = "action=add_comment&email="+$("[name=email]").val()+"&name="+$("[name=name]").val()+
			"&comment="+$("[name=comment]").val()+"&module_id="+module_id+"&post_id="+post_id+"&captcha="+$("[name=captcha]").val();
		var datata_ser = $('#comment_form').serialize();
		//alert('get:'+datata+ '\n' + 'serialize:'+datata_ser);
		$.ajax({
		  type: "POST",
		  async: false,
		  url: "/ajax.php",
			data: datata_ser,	   	
	   	success: function(ajaxresult){
	   		check_logout(ajaxresult);
	    	//alert(ajaxresult);
	    	if ($('#my_div').length > 0) {
	    		$('#my_div').html(ajaxresult);
	    	}
	    	var answer = ajaxresult.split("|!|");
			  var content = answer[2];
			  var message = answer[3];
			  
		  	$('#no_captcha, #no_name, #no_email').hide();
		  	$("#captcha").val('');
			  	
			  if (answer[1] == 1){ // ok
			  	Prepare_Comments (module_id, post_id, 1);
			  	
					$(':input', '#comment_form').each(function() {
	    			var type = this.type;
	    			var tag = this.tagName.toLowerCase();
	    			if (type == 'text' || type == 'password' || tag == 'textarea') {
	      			this.value = "";
	      		}
      		})
      		$("#no_comment").val('');
      		$('#comm_message').html('<span class="msgbox" id="msg_span" style="margin-top:20px;margin-bottom:20px;" >Коментарът Ви изчаква одобрение</span>').show();
      		
      		$('#msg_span').animate({backgroundColor:'#6282ff'}, 800);
      		//.effect("bounce", { times:3 }, 500);
      		
      		setTimeout('$("#msg_span").animate({backgroundColor:"#eeeeee"}, 1200)', 3000);
      		
      		//effect("highlight",{},3000).
			  	//$("#poll_content").html(content);
			  	//	$('#'+vote_id).addClass('active');
			  } else { // error
			  	//alert(message);
			  	var ids = message.split(",");
			  	for (var i=0; i<=ids.length; i++) {
			  		$('#'+ids[i]).show();
			  	}
			  	document['captchaimg'].src=document['captchaimg'].src+'#';
			  	//$('#vote_message').html(message);
			  	//$('#vote_message').show('slow');
			  	return false;
			  }
	   	}
		});
	}

