var http_request = false;
var zw=0;

$(document).ready(function(){
	if((navigator.userAgent.indexOf('iPhone') != -1) || (navigator.userAgent.indexOf('iPod') != -1) || (navigator.userAgent.indexOf('Android') != -1)){
		document.location = "http://mobile.frognews.bg/";
	}
	//jabaTopBanner();
	showFlashClock();
	fadeInit();
	show('body_right_photo_theme');
	if($('#news').length > 0){
		incFont(1);
		fixImages();
		//$('body').disableSelection(); 
	}
	if($('#typewriter').length > 0)check();
	if($('#joanna').length > 0)joanna_banner();
	//nov10();
	
	if($('.jcsl').length > 0)motorSlider();
	if($(".bbannerline").length > 0)scrTo();
	if($('.electslider').length > 0)electionsSlider();
	if($('.electslider_reg').length > 0)electionsRegSlider();
	if($('.zdravewidgt').length > 0){
		zdwint=window.setInterval("zdraveWidget()",3000);
	}
})



function makePOSTRequest(url, parameters, action){
		http_request = false;
		if(window.XMLHttpRequest){
			http_request = new XMLHttpRequest();
				if(http_request.overrideMimeType){
					http_request.overrideMimeType('text/html');
				}
		}else if(window.ActiveXObject){
			try{
				http_request = new ActiveXObject("Msxml2.XMLHTTP");
			}catch(e){
				try{
					http_request = new ActiveXObject("Microsoft.XMLHTTP");
				}catch(e){
					
				}
			}
		}
		if(!http_request){
			//alert('Can not connect to the webserver');
			return false;
		}
		if(action == 0){
      		http_request.onreadystatechange = ShowResult;
		}
		
		http_request.open('POST', url, true);
      	http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      	http_request.setRequestHeader("Content-length", parameters.length);
      	http_request.setRequestHeader("Connection", "close");
      	http_request.send(parameters);
}
function ShowResult(){
	if(http_request.readyState == 4){
		if(http_request.status == 200){
			result = http_request.responseText;
            eval(result);
		}
	}
}
function show(id){
	document.getElementById(id).style.display="block";
}
function hide(id){
	document.getElementById(id).style.display="none";
}
function swich(id){
	if(document.getElementById(id).style.display=="none"){
		document.getElementById(id).style.display="block";
	}else{
		document.getElementById(id).style.display="none";
	}
}

function jabaTopBanner(){
	var jabaTopBanner = new SWFObject("http://i.frognews.bg/images/banners/baner_prolet_2010.swf","jabaTopBanner",728,90,"10","#0f2b68","high");
	jabaTopBanner.addParam("wmode","transparent");
	jabaTopBanner.write("header_right_top_banner");
}
function SetTopRightBanner(url,w,h,pos){
	if(document.getElementById('body_right_swf_'+pos)){
		show('body_right_swf_'+pos);
				
		var AdvSvf = new SWFObject(url,"AdvSvf",w,h,"8","#FFFFFF","high");
		AdvSvf.addParam("wmode","transparent");
		AdvSvf.write('body_right_swf_'+pos);
	}
}
function changeWeather(){
	var val=document.getElementById('weather_sel').value;
	for(n=0;n<document.getElementById('weather_sel').options.length;n++){
		if(document.getElementById('weather_val'+n)){
			hide('weather_val'+n);
		}
	}
	show('weather_val'+val);
}
function showFlashClock(){
	var FrogNewsClock = new SWFObject("http://i.frognews.bg/images/clock/clock.swf","FrogNewsClock",57,57,"8","#FFFFFF","high");
	FrogNewsClock.addParam("wmode","transparent");
	FrogNewsClock.write("header_right_top_clock");
}

function nov10(){
	document.getElementById('nov10').style.height="100px";
	document.getElementById('nov10').style.lineHeight="100px";
	document.getElementById('nov10').style.marginBottom="20px";
	var AdvSvf2 = new SWFObject('http://i.frognews.bg/images/banners/10nov2.swf',"AdvSvf",300,100,"8","#FFFFFF","high");
	AdvSvf2.addParam("wmode","transparent");
	AdvSvf2.setAttribute("redirectUrl","http://frognews.bg/cat_105/");
	AdvSvf2.write('nov10');
	
}
function swichTabs(tab){
	if(tab == 1){
		document.getElementById('news_upcoming_nav_1').style.borderTop="5px solid #A0A0A0";
		document.getElementById('news_upcoming_nav_1').style.borderBottom="5px solid #FFFFFF";
		document.getElementById('news_upcoming_nav_2').style.borderTop="5px solid #FFFFFF";
		document.getElementById('news_upcoming_nav_2').style.borderBottom="5px solid #A0A0A0";
		hide('news_upcoming_info_2');
		show('news_upcoming_info_1');
		
	}
	if(tab == 2){
		document.getElementById('news_upcoming_nav_1').style.borderTop="5px solid #FFFFFF";
		document.getElementById('news_upcoming_nav_1').style.borderBottom="5px solid #A0A0A0";
		document.getElementById('news_upcoming_nav_2').style.borderTop="5px solid #A0A0A0";
		document.getElementById('news_upcoming_nav_2').style.borderBottom="5px solid #FFFFFF";
		hide('news_upcoming_info_1');
		show('news_upcoming_info_2');
	}
}
//////////////////////////////// PHOTO THEME ////////////////////////////////////////////////// 

var galleryId = 'body_right_photo_theme';
var	gallery;
var galleryImages;
var currentImage;
var previousImage;
var preInitTimer;
var fadeEffect = 100;
var rTime = 3000;
var fadeIntensity = 5;

preInit();

function preInit() {
	if ((document.getElementById)&&(gallery=document.getElementById(galleryId))) {
		gallery.style.visibility = "hidden";
		if (typeof preInitTimer != 'undefined')clearTimeout(preInitTimer); 
	} else {
		preInitTimer = setTimeout("preInit()",2);
	}
}
function fader(imageNumber,opacity) {
	var obj=galleryImages[imageNumber];
	if(obj){
		if (obj.style) {
			if (obj.style.MozOpacity!=null) {  
				obj.style.MozOpacity = (opacity/100) - .001;
			} else if (obj.style.opacity!=null) {
				obj.style.opacity = (opacity/100) - .001;
			} else if (obj.style.filter!=null) {
				obj.style.filter = "alpha(opacity="+opacity+")";
			}
		}
		
		if(navigator.appName=="Microsoft Internet Explorer"){
			
		}else{
			if(opacity < 99){
				//obj.childNodes[1].css({color:"#F2F2F2"});
			}else{
				//obj.childNodes[1].css({color:"#2D262A"});
			}
		}
	}
}
function fadeInit() {
	if(document.getElementById){
		preInit();
		galleryImages = new Array;
		var node = gallery.firstChild;
		while (node) {
			if (node.nodeType==1) {
				galleryImages.push(node);
			}
			node = node.nextSibling;
		}
		for(i=0;i<galleryImages.length;i++) {
			galleryImages[i].style.position='absolute';
			galleryImages[i].style.marginTop=0;
			galleryImages[i].style.zIndex=0;

			fader(i,0);
		}
		gallery.style.visibility = 'visible';
		currentImage=0;
		previousImage=galleryImages.length-1;
		opacity=100;
		fader(currentImage,100);
		window.setTimeout("crossfade(100)", rTime);
	}
}
function crossfade(opacity) {
	if(opacity <= 100) {
		fader(currentImage,opacity);
		opacity += fadeIntensity;
		window.setTimeout("crossfade("+opacity+")", fadeEffect);
	}else{
		fader(previousImage,0);
		previousImage=currentImage;
		currentImage+=1;
		if (currentImage>=galleryImages.length) {
			currentImage=0;
		}
		galleryImages[previousImage].style.zIndex = 0;
		galleryImages[currentImage].style.zIndex = 100;
		opacity=0;
		window.setTimeout("crossfade("+opacity+")", rTime);
	}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////
var fontSz=11;
function incFont(direction){
	if(fontSz < 22){
		if(direction == 1){
			document.getElementById('news').style.fontSize=(fontSz + 1)+"px";
			var elemts=document.getElementById('news').getElementsByTagName('p');
			for(a=0;a<elemts.length;a++){
				if(document.getElementById('news').getElementsByTagName('p')[a] !== null){
					if(a > 3)document.getElementById('news').getElementsByTagName('p')[a].style.fontSize=(fontSz + 1)+"px";
				}
			}
			fontSz++;
		}
	}
	if(fontSz > 9){
		if(direction == 0){
			document.getElementById('news').style.fontSize=(fontSz - 1)+"px";
			var elemts=document.getElementById('news').getElementsByTagName('p');
			for(a=0;a<elemts.length;a++){
				if(document.getElementById('news').getElementsByTagName('p')[a] !== null){
					if(a > 3)document.getElementById('news').getElementsByTagName('p')[a].style.fontSize=(fontSz - 1)+"px";
				}
			}
			fontSz--;
		}
	}	
}
function cl(id,active){
	if(active==1){
		if(id==1){
			$('#comments_blk_lnk_2').css({color:'white',backgroundColor:'#BCBDC0'});
		}else{
			$('#comments_blk_lnk_1').css({color:'white',backgroundColor:'#BCBDC0'});
		}
	}else{
		if(id==1){
			$('#comments_blk_lnk_2').css({color:'black',backgroundColor:'white'});
		}else{
			$('#comments_blk_lnk_1').css({color:'black',backgroundColor:'white'});
		}
	}
}
function clSet(id){
	if(id==1){
		document.getElementById('comments_blk_lnk_2').onmouseout="";
		$('#cm_blk_new').hide();
		$('#cm_blk_search').show();
		$('#comments_blk_lnk_2').css({color:'#FFF',backgroundColor:'#BCBDC0'});
		$('#comments_blk_lnk_1').css({color:'#BCBDC0',backgroundColor:'#FFF'});
	}else{
		document.getElementById('comments_blk_lnk_1').onmouseout="";
		$('#cm_blk_search').hide();
		$('#cm_blk_new').show();
		$('#comments_blk_lnk_1').css({color:'#FFF',backgroundColor:'#BCBDC0'});
		$('#comments_blk_lnk_2').css({color:'#BCBDC0',backgroundColor:'#FFF'});		
	}
}
function cmVal(){
	var ok=0;
	document.getElementById('cm_submit').disabled=true;
	if(navigator.appName=="Microsoft Internet Explorer"){
		if(document.getElementById('cm_comment').innerHTML.length < 3)ok=1;
	}else{
		if(document.getElementById('cm_comment').value.length < 3)ok=1;
	}

	
	if(ok==0){
		document.getElementById('cm_submit').disabled=false;
	}
}
function commSearch(nid){
	/*
	if($('cm_search').value.length > 3){
		$('cm_button').disabled=true;
		$('cm_wait').innerHTML="<img src='http://i.frognews.bg/images/img/ajax_loader.gif' />";
		window.setTimeout("commSearchNow("+nid+")",1000);
	}
	*/
}
function commSearchNow(nid){
//	makePOSTRequest('http://'+window.location.hostname+'/js/ajax/commSearch.php',"nid="+nid+"&search="+$('cm_search').value);
}
function newWinPrint(urlLoc){
	_winName3 = 'Print';
    _info  = 'toolbar=0';
    _info += ',location=0';
    _info += ',directories=0';
    _info += ',status=0';
    _info += ',menubar=0';
    _info += ',scrollbars=1';
    _info += ',resizable=1';
    _info += ',refresh the parent=1';
    _info += ',height=800';
    _info += ',width=800';
    _info += ',left=40';
    _info += ',top=30';
   DispWinPrint=window.open(urlLoc,_winName3,_info);
   return false;
}
function newWinSend(urlLoc){
	_winName4 = 'Send';
    _info  = 'toolbar=0';
    _info += ',location=0';
    _info += ',directories=0';
    _info += ',status=0';
    _info += ',menubar=0';
    _info += ',scrollbars=1';
    _info += ',resizable=1';
    _info += ',refresh the parent=1';
    _info += ',height=340';
    _info += ',width=480';
    _info += ',left=40';
    _info += ',top=30';
   DispWinSend=window.open(urlLoc,_winName4,_info);
   return false;
}
function newWinPoll(urlLoc){
	_winName5 = 'Poll';
    _info  = 'toolbar=0';
    _info += ',location=0';
    _info += ',directories=0';
    _info += ',status=0';
    _info += ',menubar=0';
    _info += ',scrollbars=1';
    _info += ',resizable=0';
    _info += ',refresh the parent=0';
    _info += ',height=700';
    _info += ',width=700';
    _info += ',left=40';
    _info += ',top=30';
   DispWinSend=window.open(urlLoc,_winName5,_info);
   return false;
}
function callPlayer(theList){
	var s2 = new SWFObject("http://i."+window.location.hostname+"/images/js/mp3player.swf", "player", "542", "100", "7");
	s2.addVariable("file",theList);
	s2.addParam('allowfullscreen','false');
	s2.addVariable("backcolor","0x000000");
	s2.addVariable("frontcolor","0xFFFFFF");
	s2.addVariable("lightcolor","0xCCCCCC");
	s2.addVariable("displayheight","0");
	s2.addVariable("width","542");
	s2.addVariable("height","100");
	s2.addVariable('showdigits','true');
	s2.addVariable('repeat','false');
	s2.addVariable('volume','90');
	s2.addVariable('showeq','false');
	s2.addVariable('wmode','transparent');
	s2.addVariable('showdownload','false');
	s2.addVariable("enablejs","true");
	s2.addVariable("autostart","false");
	s2.addVariable("javascriptid","player");
	s2.write("streamplayer");
}

function gplist(nid){
	makePOSTRequest("http://"+window.location.hostname+"/images/js/ajax/flplay.php","nid="+nid,0);
}
function clearPL(pl){
	window.setTimeout("makePOSTRequest('http://"+window.location.hostname+"/images/js/ajax/flclear.php','pl="+pl+"');",1100);
}
function joanna_banner(){	
	var j = new SWFObject("http://i.frognews.bg/images/js/player.swf","joanna",'542','433','10','#000000');
	j.addParam('allowfullscreen','false');
	j.addParam('allowscriptaccess','always');
	j.addParam('wmode','opaque');
	j.addParam('movie',"http://i.frognews.bg/images/js/player.swf");
	j.addParam('FlashVars','flv=http://i.frognews.bg/images/img/other/joana_bukovska_undercover.flv&amp;margin=0&amp;autoplay=0&amp;autoload=1&amp;showstop=1&amp;showvolume=1&amp;showiconplay=1&amp;iconplaybgalpha=35');
	j.addParam('margin','0');
	j.addParam('showstop','1');
	j.addParam('showvolume','1');
	j.addParam('showiconplay','1');
	j.addParam('iconplaybgalpha','1');
	j.write('joanna');

}
function motorSlider(){
	$('.jcsl').jcarousel({
		auto:3,
		scroll:1,
		visible:1,
		height:266,
		animation:400,
		wrap: 'circular',
		itemFallbackDimension:542
	});
}

function scrTo(){
	$(window)._scrollable();
	$('div.pane').scrollTo(0);
	$.scrollTo(0);
	$(".bbannerline").click(function(){
			$.scrollTo('#motor',800);
	});
}

function electionsSlider(){
	$('.electslider').jcarousel({
		auto:3,
		scroll:1,
		visible:3,
		height:96,
		animation:300,
		wrap: 'circular',
		itemFallbackDimension:98,
		initCallback:mycarousel_ic
	});
}
function mycarousel_ic(carousel){
	carousel.buttonNext.bind('click',function(){carousel.startAuto(0);});
	carousel.buttonPrev.bind('click',function(){carousel.startAuto(0);});
	carousel.clip.hover(function(){carousel.stopAuto();},function(){carousel.startAuto();});
};
function electionsRegSlider(){
	$('.electslider_reg').jcarousel({
		auto:5,
		scroll:1,
		visible:3,
		height:96,
		animation:300,
		wrap: 'circular',
		itemFallbackDimension:98,
		initCallback:mycarousel_ic_reg
	});
}
function mycarousel_ic_reg(carousel){
	carousel.buttonNext.bind('click',function(){carousel.startAuto(0);});
	carousel.buttonPrev.bind('click',function(){carousel.startAuto(0);});
	carousel.clip.hover(function(){carousel.stopAuto();},function(){carousel.startAuto();});
};

function fixImages(){
	var images=$('#news img');
	
	for(var i=0;i<images.length;i++){
		var w=$(images[i]).width;
		if($(images[i]).width() > 542){
			$(images[i]).css({width:'542px',marginLeft:'0px'});
		}else{
			if($(images[i]).width() < 530 ){
				$(images[i]).css({display:'block',marginRight:'15px',marginBottom:'2px'});
			}
		}
	}
}

function zdraveWidget(){
	
	$('.zdravewidgt_top,.zdravewidgt_cn').mouseover(function(){
		
		window.clearInterval(zdwint);
		zdwint="";
		
		$('.zdravewidgt_top div a.tl_1').click(function(){
			$('.zdravewidgt_top div a').removeClass('sel');
			$(this).addClass('sel');
			$('.zdravewidgt_cn_sl_mover').css({marginLeft:'0px'});
			return false;
		})
		$('.zdravewidgt_top div .tl_2').click(function(){
			$('.zdravewidgt_top div a').removeClass('sel');
			$(this).addClass('sel');
			$('.zdravewidgt_cn_sl_mover').css({marginLeft:'-280px'});
			return false;
		})
		$('.zdravewidgt_top div .tl_3').click(function(){
			$('.zdravewidgt_top div a').removeClass('sel');
			$(this).addClass('sel');
			$('.zdravewidgt_cn_sl_mover').css({marginLeft:'-560px'});
			return false;
		})
		
		
	}).mouseout(function(){
		window.clearInterval(zdwint);
		zdwint="";
		zw=0;
		window.setTimeout("zdrwstart()",500);
	})
	
	$('.zdravewidgt_top div a').removeClass('sel');
	$('.zdravewidgt_top div .tl_'+(zw+1)).addClass('sel');
	$('.zdravewidgt_cn_sl_mover').animate({marginLeft:'-'+(280*(zw))+'px'},700);
	
	if(zw > 1){
		zw=0;
	}else{
		zw++;
	}
	
}
function zdrwstart(){
	window.clearInterval(zdwint);
	zdwint="";
	zw=0;
	zdwint=window.setInterval("zdraveWidget()",3000);
}

	

jQuery.fn.extend({
	disableSelection:function(){
		$(this).bind("contextmenu",function(e){
			e.preventDefault();
		});
		return this.each(function(){
			this.onselectstart=function(){return false;};
			this.unselectable="on";
			jQuery(this).css('user-select','none');
			jQuery(this).css('-o-user-select','none');
			jQuery(this).css('-moz-user-select','none');
			jQuery(this).css('-khtml-user-select','none');
			jQuery(this).css('-webkit-user-select','none');
		});
	}
});
