/* toggle an element's display */
function toggle(nr,way){
	var current = false;
	if(way) current = way;
	if(document.getElementById){
		if(!current) current=(document.getElementById(nr).style.display=='none')?'block':'none';
		document.getElementById(nr).style.display=current;
	}else if(document.layers){
		if(!current) current=(document.layers[nr].display=='none')?'block':'none';
		document.layers[nr].display=current;
	}else if(document.all){
		if(!current) current=(document.all[nr].style.display=='none')?'block':'none';
		document.all[nr].style.display=current;
	}
	document.body.focus();
} 

/* get, set, and delete cookies */
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 ) {
	var today = new Date();
	today.setTime( today.getTime() );
	if ( expires ) {
		expires = expires * 1000 * 60 * 60 * 24;
	}
	var expires_date = new Date( today.getTime() + (expires) );
	document.cookie = name+'='+escape( value ) +
		( ( expires ) ? ';expires='+expires_date.toGMTString() : '' ) + //expires.toGMTString()
		( ( path ) ? ';path=' + path : '' ) +
		( ( domain ) ? ';domain=' + domain : '' ) +
		( ( secure ) ? ';secure' : '' );
}
	
function deleteCookie( name, path, domain ) {
	if ( getCookie( name ) ) document.cookie = name + '=' +
		( ( path ) ? ';path=' + path : '') +
		( ( domain ) ? ';domain=' + domain : '' ) +
		';expires=Thu, 01-Jan-1970 00:00:01 GMT';
}
function fixHomePage(obj){
    var homepage = "http://comments.ua";
    if (obj.style.behavior) obj.setHomePage(homepage);
    else if (netscape
              && netscape.security
              && netscape.security.PrivilegeManager
              && navigator.preference)
    {
        netscape.security.PrivilegeManager.enablePrivilege("UniversalPreferencesWrite");
        navigator.preference("browser.startup.homepage", homepage);
    }
}
function addBookmark(title,url) {
	if (window.sidebar) {
		window.sidebar.addPanel(title, url,"");
	} else if( window.opera && window.print ) {
		var t = document.createElement('a');
		t.setAttribute('rel', 'sidebar');
		t.setAttribute('href', url);
		t.setAttribute('title', title);
		t.click();
	} else if( document.all ) {  
		window.external.AddFavorite( url, title);  
	}
}
function checkMail(it) {
	var x = it.value;
	var flt  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (flt.test(x)) return true;
	return false;
}
function openw(url,wi,hi) {
	if(wi==undefined) wi = 670;
	if(hi==undefined) hi = 690;
	window.open(url,'popup','top=100, left=100,fullscreen=no,resizable=no,toolbar=no,location=no,directories=no,status=no,menubar=no,width='+wi+',height='+hi+',scrollbars=no').focus();
	return false;
} 
function grabIt(it) {
	if(window.clipboardData) {
		if(window.clipboardData.setData('text',$('#'+it).html())) alert('Код скопирован в буфер обмена');
		else alert('Unexpected error...');
	}else alert('Ваш браузер не поддерживает данную функцию, пожалуйста используйте Ctrl-C');
} 
function jumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}
function checkdate(d,m,y) {
    return m > 0 && m < 13 && y > 0 && y < 32768 && d > 0 && d <= (new Date(y, m, 0)).getDate();
}
$(document).ready(function(){
    $('#jclock').jclock();
}); 

(function($) {
  $.fn.jclock = function(options) {
    var version = '2.2.0';
    var opts = $.extend({}, $.fn.jclock.defaults, options);
    return this.each(function() {
      $this = $(this);
      $this.timerID = null;
      $this.running = false;
      $this.increment = 0;
      $this.lastCalled = new Date().getTime();
      var o = $.meta ? $.extend({}, opts, $this.data()) : opts;
      $this.format = o.format;
      $this.utc = o.utc;
      $this.utcOffset = (o.utc_offset != null) ? o.utc_offset : o.utcOffset;
      $this.seedTime = o.seedTime;
      $this.timeout = o.timeout;
      $this.css({
        fontFamily: o.fontFamily,
        fontSize: o.fontSize,
        backgroundColor: o.background,
        color: o.foreground
      });
      $.fn.jclock.startClock($this);
    });
  };
  $.fn.jclock.startClock = function(el) {
    $.fn.jclock.stopClock(el);
    $.fn.jclock.displayTime(el);
  }
  $.fn.jclock.stopClock = function(el) {
    if(el.running) {
      clearTimeout(el.timerID);
    }
    el.running = false;
  }
  $.fn.jclock.displayTime = function(el) {
    var time = $.fn.jclock.getTime(el);
    el.html(time);
    el.timerID = setTimeout(function(){$.fn.jclock.displayTime(el)},el.timeout);
  }
  $.fn.jclock.getTime = function(el) {
    if(typeof(el.seedTime) == 'undefined') {
      var now = new Date();
    } else {
      el.increment += new Date().getTime() - el.lastCalled;
      var now = new Date(el.seedTime + el.increment);
      el.lastCalled = new Date().getTime();
    }
 
    if(el.utc == true) {
      var localTime = now.getTime();
      var localOffset = now.getTimezoneOffset() * 60000;
      var utc = localTime + localOffset;
      var utcTime = utc + (3600000 * el.utcOffset);
      now = new Date(utcTime);
    }
    var timeNow = "";
    var i = 0;
    var index = 0;
    while ((index = el.format.indexOf("%", i)) != -1) {
      timeNow += el.format.substring(i, index);
      index++;
      var property = $.fn.jclock.getProperty(now, el, el.format.charAt(index));
      index++;
      timeNow += property;
      i = index
    }
    timeNow += el.format.substring(i);
    return timeNow;
  };
  $.fn.jclock.getProperty = function(dateObject, el, property) {
    switch (property) {
      case "H": // hour as a decimal number using a 24-hour clock (range 00 to 23)
          return ((dateObject.getHours() < 10) ? "0" : "") + dateObject.getHours();
      case "M": // minute as a decimal number
          return ((dateObject.getMinutes() < 10) ? "0" : "") + dateObject.getMinutes();
      case "%":
          return "%";
    }
  }
  $.fn.jclock.defaults = {
    format: '%H:%M',
    utcOffset: 0,
    utc: false,
    fontFamily: '',
    fontSize: '',
    foreground: '',
    background: '',
    seedTime: undefined,
    timeout: 15000 // 1000 = one second, 60000 = one minute
  };
})(jQuery);

 function intval( mixed_var, base ) {  
	var tmp;  
	if( typeof( mixed_var ) == 'string' )
	{  
		tmp = parseInt(mixed_var*1);  
		if(isNaN(tmp) || !isFinite(tmp)){  
			return 0;  
		} else{  
			return tmp.toString(base || 10);  
		}  
	} else if( typeof( mixed_var ) == 'number' && isFinite(mixed_var) ){  
		return Math.floor(mixed_var);  
	} else{  
		return 0;  
	}  
}  

var cur_top = 1;
var topiTimer = '';

function NewsTrigger()
{ 
	cur_top = cur_top<4 ? cur_top + 1 : 1;
	$("#top_page_" + cur_top).trigger('click'); 
}

function startNewsInt()
{
	topiTimer = setTimeout("NewsTrigger()" , 15000); 
} 

function stopNewsInt() { 
	clearTimeout(topiTimer); 
} 

$(document).ready(function(){ 
	$(".top_page").click(function() {
		
		stopNewsInt();
		
		var page = $(this).attr("rel");
		$.each([1,2,3,4], function(i, v) {
			if(v==page) {
				$("#top_page_"+v).addClass("link_active_preview");
				cur_top = v;
			}else {
				$("#top_page_"+v).removeClass("link_active_preview");
			}
		});
		$("#top_line").animate({marginLeft: "-"+(page-1)*468+"px"},"slow",function(){
			startNewsInt();	
			return false;
		});
		
	});
	
	startNewsInt();
	 
	$('.top_page').hover( function() { 
		stopNewsInt();
		} 
	);  
	
	
	$('.top_page').mouseout(function() {
		stopNewsInt();
		startNewsInt(); 
		}
	);
	 
});

