function constructLightBox()
{
	$("a[rel*=lightbox]").fancybox({
		'imageScale': false,
		'centerOnScroll': false
	});
}
$(document).ready(
	function()
	{
		if( $("#submenu").length == 0 )
		{
			$("#menu").addClass("only");
		}
		//var height = Math.max(parseFloat($("#sidebar").css("height")), parseFloat($("#mainbar").css("height")));
		//$("#mainbar").css("height", height+"px");
					
		$("#tab_commented").bind("click", function(){
			$("#data_commented").show();
			$("#data_archive").hide();
			$(".tabs .tab").removeClass("active");
			$(this).addClass("active");
			return false;
		});

		$("#tab_archive").bind("click", function(){
			$("#data_commented").hide();
			$("#data_archive").show();
			$(".tabs .tab").removeClass("active");
			$(this).addClass("active");
			return false;
		});

		constructLightBox();
		
		$("a.external").each(function(){
			$(this).attr("href",$(this).attr("title"));
			$(this).removeAttr("title");
			$(this).removeAttr("class");
		});

		
		function checksize( i, as )
		{
			if( i == 0 )
				$("#font-smaller").addClass("disabled");
			else if( i == as )
				$("#font-bigger").addClass("disabled");
		}
		
		var size = ['1.2','1.3','1.45','1.6','1.7'];
		var as = size.length;
		if( $.cookie('fs') ) 
			i = $.cookie('fs'); 
		else 
			i = 0; 
		$("#text").css("fontSize", size[i]+'em');
		
		checksize( i, as );
		
		$("#font-bigger").bind("click", function(){
			for(i=0; i<as; i++) 
			{				
				if ($("#text").css("fontSize") == size[i] + 'em') {
					if( i < as-1 ) i++;
					$("#text").css("fontSize", size[i] + 'em');
					$.cookie('fs', i, {path: '/'});
					$("#font-smaller").removeClass("disabled");
					checksize( i, as-1 );
					break;
				} 
			}
			return false;
		});
		
		$("#font-smaller").bind("click", function(){			
			for(i=0; i<=4; i++) 
			{				
				if ($("#text").css("fontSize") == size[i] + 'em') {
					if( i > 0 ) i--;
					$("#text").css("fontSize", size[i] + 'em');
					$.cookie('fs', i, {path: '/'});
					$("#font-bigger").removeClass("disabled");
					checksize( i, as );
					break;
				} 
			}
			return false;			
		});

		$("#forms input[type=submit]").attr("disabled", "disabled");
		$("#forms textarea").bind("keyup",function(){
			if( $(this).val().length > 0 )
				$("#forms input[type=submit]").removeAttr("disabled");
			else
				$("#forms input[type=submit]").attr("disabled", "disabled");
		});
		
		$(".js-hidden").hide();  // то что должно включаться Javascript'ом
		$(".js-visible").show(); // контроллер js, который переключает .js-hidden

	    $('a[href$="mp3"]').flash(
	        { src: '/-/plugins/jquery-flash/player.swf', height: 20, width: 450 },
	        { version: 7 },
	        function(htmlOptions) {
	            $this = $(this);
	            
	            htmlOptions.flashvars.file = $this.attr('href');
	            $this.before($.fn.flash.transform(htmlOptions));
	            
	            $this.hide();						
	        }
	    );

	    $('a[href$="flv"]').flash(
	        { src: '/-/plugins/jquery-flash/player.swf', height: 225, width: 400, allowfullscreen: true },
	        { version: 7 },
	        function(htmlOptions) {
	            $this = $(this);
	            
	            htmlOptions.flashvars.file = $this.attr('href');
	            $this.before($.fn.flash.transform(htmlOptions));
	            
	            $this.hide();						
	        }
	    );

	    $('a[href*="youtube.com"]').flash(
	        { src: '/-/plugins/jquery-flash/player.swf', height: 225, width: 400, allowfullscreen: true },
	        { version: 7 },
	        function(htmlOptions) {
	            $this = $(this);
	            
	            htmlOptions.flashvars.file = $this.attr('href');
	            $this.before($.fn.flash.transform(htmlOptions));
	            
	            $this.hide();						
	        }
	    );

	}
);

function GetCookie(name) {
    var start = document.cookie.indexOf(name+"=");
    var len = start+name.length+1;
    if ((!start) && (name != document.cookie.substring(0,name.length))) return null;
    if (start == -1) return null;
    var end = document.cookie.indexOf(";",len);
    if (end == -1) end = document.cookie.length;
    return unescape(document.cookie.substring(len,end));
}

function SetCookie(name,value,expires,path,domain,secure) {
    document.cookie = name + "=" +escape(value) +
        ( (expires) ? ";expires=" + expires.toGMTString() : "") +
        ( (path) ? ";path=" + path : ";path=/") + 
        ( (domain) ? ";domain=" + domain : "") +
        ( (secure) ? ";secure" : "");
}

function submitVote(vote_id, msg)
{
	var today = new Date();
    var expires = new Date(today.getTime() + (100 * 86400000))
    var _answer = '';

    for (var i=0; i < document.pollarea.poll.length; i++)
    {
       if (document.pollarea.poll[i].checked) _answer = document.pollarea.poll[i].value;
	}
	
	if (_answer == '')
	{
	   alert(msg);
	   return false;
	}
	SetCookie('poll',vote_id,expires);
    JsHttpRequest.query(
      '/poll/',
      {
        'poll_id': vote_id,  
        'choice_id': _answer
      },
      function(result, errors) {
        document.getElementById("poll").innerHTML = result["result"];
      },
      true
    );	
}

/*
 * Ð£ÑÑÐ°Ð½Ð°Ð²Ð»Ð¸Ð²Ð°ÐµÐ¼ ÑÑÐ°ÑÑÐ¾Ð²ÑÑ ÑÑÑÐ°Ð½Ð¸ÑÑ
 */
function SetHomePage( $url, $desc ) 
{
    try{
        var objSrc = document.getElementById('jshomepage');
        if(!objSrc) return false;
        var netscapePriv = window.netscape;
        if (netscapePriv && netscapePriv.security && netscapePriv.security.PrivilegeManager && navigator.preference) 
        { //Mozilla
            netscapePriv.security.PrivilegeManager.enablePrivilege("UniversalPreferencesWrite");
            navigator.preference("browser.startup.homepage",$url);
        }
        else
        { //others. Originaly MSIE

                objSrc.setHomePage($url);
        }
    }
    catch(e)
    {
        if(window.sidebar) {
        window.sidebar.addPanel($desc, $url, '');
        }
        else
        {
        //alert('ÐÑÐ¸Ð±ÐºÐ°: '+e.message);
        }
    }
    
	return false;
}

function setPrintCSS(isPrint) {
  if (document.getElementsByTagName)
      x = document.getElementsByTagName('link');
  else if (document.all)
      x = document.all.tags('link');
  else
  {
      alert('Простите, этот скрипт не работает в вашем броузере');
      return;
  }
  for (var i=0;i<x.length;i++) {
      if(x[i].title == 'printview'){x[i].disabled = !isPrint;}
      if(x[i].title == 'screenview'){x[i].disabled = isPrint;}
  }
}

document.onkeydown = function (e)
{
	if (!e)
	{
		e = window.event;
	}
	if (e.ctrlKey) //arrow navigation
	{
		var code = e.keyCode ? e.keyCode : (e.which ? e.which : 0);
		var arrow = code == 37 ? document.getElementById('prev_page') : (code == 39 ? document.getElementById('next_page') : 0);
		if (arrow)
		{
			location.href = arrow.href
		}
	}
}

function tagToggle(value)
{
	$('#short').toggle();
	$('#full').slideToggle();
	$.cookie('tag_toggle', value, { path: '/'});
	
	return false;
}

function checkForm()
{
	if ( $('#username').attr('value') == '' )
	{
		$('#username').addClass('warning');
		return false;
	}
	else
	{
		$.cookie('username', $('#username').attr('value'), { path: '/', domain: '.vidomosti-ua.com' });
	}

	if ( $('#email').attr('value') == '' )
	{
		$('#email').addClass('warning');
		return false;
	}
	else
	{
		$.cookie('email', $('#email').attr('value'), { path: '/', domain: '.vidomosti-ua.com' });
	}
	
	if ( $('#commentary').attr('value') == '' )
	{
		$('#commentary').addClass('warning');
		return false;
	}
	
	return true;
}