$(document).ready(function() {
	
	// mazasis akciju baneris
	setInterval('change_campaign_small()', 7000);
	

	$('ul.main_menu>li').hover(
		function() {
			if ($(this).hasClass('selected'))
				{}
			else
				{$(this).addClass('hover');}
		}, 
		function() {
			$(this).removeClass('hover');
		}
	
	);
	
	$('ul.main_menu ul li.last').hover(
		function(){
			$(this).addClass('last_hover');
		},
		function(){
			$(this).removeClass('last_hover');
		}
	);
	
	$('ul.main_menu>li.selected').hover(
		function(){
			$(this).children('ul').show();
		},
		function(){
			$(this).children('ul').hide();
		}
	);
	
	$('ul.list_1 li a').hover(
		function(){
			var obj = {
				'color': 'white',
				'text-decoration': 'none'
			}; 
			$(this).parent('li').children('a').css(obj);
		},
		function(){
			var obj = {
				'color': '#62BCFF',
				'text-decoration': 'underline'
			}; 
			$(this).parent('li').children('a').css(obj);
		}
	);
	
	add_vote = function(video, user_id, vote)
	{		
	    $.post(
	        base_url + index_page + 'u-pajegos/vote',
	        { video: video, user_id: user_id, vote: vote },
	        function(response)
	        {   
	        	if(response == 'klaida')
	        		{
	        		$('.voter-voted').html("Jau balsavote.");
	        		}
	        	else
	        		{
	        		if (vote == '1')
	        			{
		        			$('#vote_like').html(response);
	        			}
	        		else
	        			{
		        			$('#vote_dislike').html(response);
		        		}
	        		}
	        	
	        	// redirect to next video
	        	var next_url = $("a.go_right").attr("href");
	        	if (typeof(next_url) != "undefined"){
	        		location.href = next_url;
	        	}
	        }
	    );
	};
	
	scale_bg = function(){
		$('.scaled_bg').each(function(){
			var obj = $(this);
			var scale_height = $(obj).height();
			$(obj).append('<img class="bgs" src="'+base_url+'public/media/gradients/scaled_bg.png" alt="" />');
			//$(obj).append('<img class="bgs_decor" src="'+base_url+'public/media/gradients/scla_bg_l_dec.jpg" alt="" width="275" height="154" />');
			$(obj).children('img.bgs').height(scale_height);
			if ($.browser.msie) 
				{
					$('img.bgs').hide()
				}
			else
				{
					$(obj).children('img.bgs').show();
					$(obj).children('img.bgs_decor').show();
				};
		});
	};
	
	scale_bg();
	
/*REGISTRATION POPUP*/
$('.connect_but').live('click', function () { 
	
	$('body').append('<div id="TB_overlay" class="TB_overlayBG" onclick="remove_popups()"></div><div class="popup_layer"><div class="pop_position_fix"></div></div>');
	if ($.browser.msie) 
	{}
	else
	{
		$('#TB_overlay').css({opacity: .0}).animate({opacity: .65}, 500);
	};
	$('.pop_position_fix').load(base_url + cur_lang +'/users/login_form', function(){
		$('.pop_content').hide();
		$('.pop_content').show('fast');
	});
});

$('.call_regs').live('click', function () {
	$('.pop_content').hide('fast', function(){$(this).remove()});
	$('.pop_position_fix').load(base_url + cur_lang + '/users/registration_form', function(){
		$('.pop_content').hide();
		$('.pop_content').show();
		$('.styled_select').sSelect({ddMaxHeight: '250px'});
	});
});

$('.call_log').live('click', function () { 
	$('.pop_content').hide('fast', function(){$(this).remove()});
	$('.pop_position_fix').load(base_url + cur_lang +'/users/login_form', function(){
		$('.pop_content').hide();
		$('.pop_content').show();
	});
});

$('.call_data_edit').live('click', function () { 
	$('body').append('<div id="TB_overlay" class="TB_overlayBG" onclick="remove_popups()"></div><div class="popup_layer"><div class="pop_position_fix"></div></div>');
	if ($.browser.msie) 
		{}
	else
		{
			$('#TB_overlay').css({opacity: .0}).animate({opacity: .65}, 500);
		};
	$('.pop_position_fix').load(base_url + cur_lang +'/users/edit', function(){
//		$('.pop_content').hide();
		$('.pop_content').show('fast');
		$('.styled_select').sSelect({ddMaxHeight: '250px'});
	});
});



$('.call_forgot_password').live('click', function () { 
	$('.pop_content').hide('fast', function(){$(this).remove()});
	$('.pop_position_fix').load(base_url + cur_lang +'/users/forgot_password_form', function(){
		$('.pop_content').hide();
		$('.pop_content').show();
	});
});

remove_popups = function()
	{
		$('.pop_content').hide('fast');
		if ($.browser.msie) 
			{
				$('#TB_overlay').remove();	
			}
		else
			{
				$('#TB_overlay').animate({opacity: .0}, 500, function(){
					$(this).remove();
				});
			};
		$('.popup_layer').hide(500, function(){
			setTimeout(function(){ 
				$('.popup_layer').remove();
			},600);
		});
	};
	
$('ul.list_3 li a').hover(
		function(){
			$(this).children('.play_ico').hide()
		},
		function(){
			$(this).children('.play_ico').show()
		}
	);
	
	
$('#slider_block').after('<div id="nav">')
	 .cycle({ 
		fx:    'scrollHorz', 
		delay: 0,
		timeout: 4500,
		cleartype: true,
		pager:  '#nav',
		cleartype: true,
		pause: true,
		cleartypeNoBg: true,
		next: 'a.next_sl',
		prev: 'a.prev_sl'
	});

	 $('#nav a').html('');
	 
	 
});

function fb_share_dark_campaign() {
  var u = 'http://www.utenosalus.lt/lt/dark-akcija-baruose?utm_source=facebook&utm_medium=fb-share&utm_campaign=dark-baruose';
  window.open('http://www.facebook.com/sharer.php?u='+escape(u),'sharer','toolbar=0,status=0,width=626,height=436');
  return false;
}



//Start Javascript communicate with Flash //
function getFlashMovieObject(movieName){
	if (window.document[movieName]){
		return window.document[movieName];
	}
	if (navigator.appName.indexOf("Microsoft Internet")==-1){
		if (document.embeds && document.embeds[movieName])
			return document.embeds[movieName];
	}
	else{
		return document.getElementById(movieName);
	}
}
// End Javascript communicate with Flash //


function change_campaign_small(){
	var campaign_small = $("ul.campaign_small");
	
	var display = campaign_small.css('margin-left');
	if (typeof(display) != "string"){
		return null;
	}
	
	
	margin_left = parseInt(campaign_small.css('margin-left').replace('px', '').replace('auto', '0'));

	next_li = margin_left * -1 + 235;
	
	if (campaign_small.width() > next_li){
		margin_left = next_li;
    }else{
		margin_left = 0;
    }

	campaign_small.animate({
         marginLeft:'-'+ margin_left +'px' }, 1000);
}


function facebook_share_url (url){
    var share = {
        method: 'stream.share',
        u: url
    };
    FB.ui(share);
}


function facebook_share_text (name, desc, link, picture){

	var share = {
        method: 'feed',
        name: name,
        link: base_url + link,
        picture: base_url + picture,
        caption: ' ',
        description: desc,
        message: ''
    };
    FB.ui(share); // second parameter can be callback
}


// input allow only numbers, onkeypress
function my_numbers_only(e, allowed_additional) {
	var key;
	var char;
	var allowed = "0123456789";
	
	if (typeof(allowed_additional) != "undefined"){
		allowed = allowed + allowed_additional;
	}
	
	if (window.event){
	   key = window.event.keyCode;
	}else if (e){
	   key = e.which;
	}
	else {
	   return true;
	}
	
	char = String.fromCharCode(key);
	
	// control keys
	if ((key==null) || (key==0) || (key==8) ||  (key==9) || (key==13) || (key==27) ){
	   return true;
	}
	else if ( (allowed.indexOf(char) > -1) ){
	   return true;
	}
	else{
	   return false;
	}
}


