/**
 * This project is created by
 * PILLAX (http://www.pillax.com)
 */

$(function(){

	/***************************************************************************
	 * News on index
	 * change entry on mouse over
	 */
	$('div#index_news_paginator a').hover(
		function(){
			$('div.index_news_entry').css('display', 'none');
			$('div#index_news_entry_' + $(this).attr('rel')).css('display', 'block');
			$('div#index_news_paginator a').removeClass('box_pagiantor_active');
			$(this, 'a').addClass('box_pagiantor_active');
		},
		function(){
			
		}
	);

	$('div#index_news_paginator a').click(function(){
		$(this).focus().blur();
		return false;
	});


	/***************************************************************************
	 * Carousel
	 */
	$('div.carousel_controls a').click(function(){
		$(this).focus().blur();
		return false;
	});

	$('#vip_firms').jCarouselLite({
		btnNext: "#vip_firms_carousel_right",
		btnPrev: "#vip_firms_carousel_left",
		visible: 1
	});

	$('#last_machines_offers').jCarouselLite({
		btnNext: "#last_machines_offers_carousel_right",
		btnPrev: "#last_machines_offers_carousel_left",
		visible: 1
	});


	/***************************************************************************
	 * Votes
	 */
	$('.vote_up').live("click", function(){
		element_id = $(this).attr('rel');
		$.get('ajax_vote_action.php',
			  {id:element_id, type:'comment', vote:1}, 
			  function(data){
				$('div#result_' + element_id).html(data);
			  }
		)
		deactivate_vote_box(element_id);
		return false;
	});

	$('.vote_dwn').live("click", function(){
		element_id = $(this).attr('rel');
		$.get('ajax_vote_action.php',
			  {id:element_id, type:'comment', vote:0}, 
			  function(data){
				$('div#result_' + element_id).html(data);
			  }
		)
		deactivate_vote_box(element_id);
		return false;
	});


	/***************************************************************************
	 * Comments
	 */
	$('div#comments div.pager a').live("click", function(){
		var page		= $(this).attr('rel');
		var obj_group	= $('input#obj_group').attr('value');
		var obj_id		= $('input#obj_id').attr('value');

		load_comments(page, obj_group, obj_id);

		$('input#page_num').val(page);

		return false;
	});


	/***************************************************************************
	 * Video tmb rotator
	 */
	var t;
	var thmb_id = 2;

	$("a.video_tmb_lnk").hover(
		function()
		{
			var img_id				= $(this).attr("rel");
			var iterations_count	= $(this).attr("title");
			var link				= $(this);
			var type				= $(this).attr("alt");

			// pokazvame vednaga sled kato usera hover-ne snimkata
			$("img.video_tmb", link).attr("src", "pi_files/vidtmb" + type + "_" + img_id + "_" + thmb_id + ".gif");

			t = setInterval(function()
			{
				thmb_id++;
				$("img.video_tmb", link).attr("src", "pi_files/vidtmb" + type + "_" + img_id + "_" + thmb_id + ".gif");
				if (thmb_id == iterations_count) {
					thmb_id = 0;
				}

			}, 500);
		},

		function()
		{
			clearInterval(t);
			var img_id = $(this).attr("rel");
			thmb_id = 2;
			$("img.video_tmb", this).attr("src", "pi_files/vidtmb" + type + "_" + img_id + "_1.gif");
		}
	);


	/***************************************************************************
	 * Filters action
	 */
	$('div#filter select').change(function(){
		$('form#filter_form').submit();
	});


	/***************************************************************************
	 * Image carousel
	 */
	$('div#img_paginator_container').jCarouselLite({
		btnNext: "#img_paginator_carousel_right",
		btnPrev: "#img_paginator_carousel_left",
		visible: 6,
		circular: false
	});

	$('div#img_paginator a').click(function(){
		$(this).focus().blur();
		return false;
	});

	$('div#img_paginator_container a').click(function(){
		var img = $(this).attr('rel');
		var title = $('img', this).attr('alt');
		var description = $('img', this).attr('title');

		$('div#img_container').css('background-image', 'url("pi_files/big_' + img + '")');
		$('div#img_container span b').text(title);
		$('div#img_container span i').text(description);

		$(this).focus().blur();
		return false;
	});

	$('div#img_container').hover(
		function()
		{
			//$('div#img_container span, div#img_container a').css('display', 'block');
			$('div#img_container span').css('display', 'block');
		},

		function()
		{
			//$('div#img_container span, div#img_container a').css('display', 'none');
			$('div#img_container span').css('display', 'none');
		}
	);

	


	/***************************************************************************
	 * Row with special hover efect
	 */
	$('.bckg_hover').hover(
		function(){
			$(this).css('background-color', '#f1f1f3');
		},
		function(){
			$(this).css('background-color', '#ffffff');
		}
	);


	/**
	 * Delete entry confirmation
	 */
	$('.delete').click(function(){

		if(window.confirm('Да изтривам ли?') == true)
		{
			return true;
		}

		return false;
	})




})









function load_comments(page_val, obj_group_val, obj_id_val)
{
	$.get('ajax_load_comments.php',
		  {page:page_val, 
		   obj_group:obj_group_val, 
		   obj_id:obj_id_val}, 
		  function(data){
			$('div#comments_container').html(data);
		  }
	)
}


function deactivate_vote_box(element_id)
{
	$('#vote_box_' + element_id).css('display', 'none');
	$('#vote_box_' + element_id + '_inactive').css('display', 'block');
}


function insertAtCaret(areaId,text)
{
	var txtarea = document.getElementById(areaId); 
	var scrollPos = txtarea.scrollTop; 
	var strPos = 0; 
	var br = ((txtarea.selectionStart || txtarea.selectionStart == '0') ? "ff" : (document.selection ? "ie" : false ) ); 
	if (br == "ie") 
	{
		txtarea.focus();
		var range = document.selection.createRange();
		range.moveStart ('character', -txtarea.value.length);
		strPos = range.text.length;
	} 
	else if (br == "ff") strPos = txtarea.selectionStart;

	var front = (txtarea.value).substring(0,strPos);
	var back = (txtarea.value).substring(strPos,txtarea.value.length);
	txtarea.value=front+text+back;
	strPos = strPos + text.length;
	if (br == "ie") 
	{
		txtarea.focus();
		var range = document.selection.createRange();
		range.moveStart ('character', -txtarea.value.length);
		range.moveStart ('character', strPos);
		range.moveEnd ('character', 0);
		range.select();
	}
	else if (br == "ff") 
	{
		txtarea.selectionStart = strPos;
		txtarea.selectionEnd = strPos;
		txtarea.focus();
	}
	txtarea.scrollTop = scrollPos;
}
