function setHomepage()
{
 if (document.all)
    {
        document.body.style.behavior='url(#default#homepage)';
        document.body.setHomePage('http://www.nbaworld.lt');
 
    }
    else if (window.sidebar)
    {
    if(window.netscape)
    {
         try
   {  
            netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");  
         }  
         catch(e)  
         {  
        	 alert("Šio veiksmo vykdymas buvo uždraustas jūsų naršyklėje. Jeigu norite įjungti prašome įvesti about:config adreso eilutėje ir pakeisti signed.applets.codebase_principal_support reikšmę į TRUE.");  
         }
    } 
    var prefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components. interfaces.nsIPrefBranch);
    prefs.setCharPref('browser.startup.homepage','http://www.nbaworld.lt');
 }
}


function addJavascript(jsname,pos) {
	var th = document.getElementsByTagName(pos)[0];
	var s = document.createElement('script');
	s.setAttribute('type','text/javascript');
	s.setAttribute('src',jsname);
	th.appendChild(s);
} 

function isNumber(x){
  if (x == parseFloat(x)){
  	return true;
  }else{
    return false;
  }	
}

function initBetCalculator(){

 $("#betCredits").keyup(function (e) {
  var betCredits=$('#betCredits').val();
  var rate=$('#rateTotal').val();
  if (isNumber(betCredits)){
  	value = rate * betCredits;
    $("#victoryCredits").val(value.toFixed(0));
  } 
 });
 
 
}


function reload_tb(title,link){
	$("#TB_imageOff").unbind("click");
	$("#TB_closeWindowButton").unbind("click");
	$("#TB_window").fadeOut(
			"fast",function(){
				$('#TB_window,#TB_overlay,#TB_HideSelect').trigger("unload").unbind().remove();
				$("#TB_load").remove();
				if (typeof document.body.style.maxHeight == "undefined") {//if IE 6
					$("body","html").css({height: "auto", width: "auto"});
					$("html").css("overflow","");
				}
				document.onkeydown = "";
				document.onkeyup = "";
				tb_show(title,link, false);
			});
}


function advertisement_left_side_reload(height){
	$.get("advertisement_left_side.php?height="+height, function(data){
		$("#advertisiment_left_side").fadeOut("slow", function(){
			$("#advertisiment_left_side").html(data);
			$("#advertisiment_left_side").fadeIn("slow");
			$("a.showNews").trigger("unload").unbind().click(
					function(event){		
						event.preventDefault();	
						lodeMainText($(this).attr("href"));
						return false;
					});	
			
		});
		
			
	});
}






function initLinks(){
	initBetCalculator();
	
	$("a.thickbox, area.thickbox, input.thickbox").unbind("click");
	tb_init('a.thickbox, area.thickbox, input.thickbox');
	if (tb_visible==false){
		$("#comment").css("visibility","visible");
		$("#comment").markItUp(mySettings);
		$("#addblog").markItUp(blogItemEditSettings);
		$("#comment").css("visibility","visible");

		$("#sendmessage").markItUp(sendMessageSettings);
		$("#sendmessage").css("visibility","visible");
	}
	
	$(".quateComment").click(function() {
		//href = "ddd";
		id=$(this).attr("alt");
		commentMod=$("#commentSendForm #mod").val();
		quateWriter=($(this).parents(".commentItem").find(".commenAuthor").html());
		$.post("user.php?mod=m_users&type=110&id="+id,
				{
				change: "true",
				action: "editComment",
				commentMod: commentMod },
				function(data)
				{
					if(data.sucess!="true"){
						alert(data.message);	
					}else{
					
						
						$.markItUp(
								{     target:'#comment',
									  openWith:'[quote][b]'+quateWriter+' rašė:[/b]\n'+data.message+'[/quote]',
									  closeWith:'',
									  placeHolder:'' }

						);
					}
				}, "json");	
		/*
		quateWriter=($(this).parents(".commentItem").find(".commenAuthor").html());
		quate=($(this).parents(".commentItem").find(".commentText").html());
		text = "[b]"+quateWriter+" rašė:[/b]\n"+quate;
		$.markItUp(
				{     target:'#comment',
					  openWith:'[quote]',
					  closeWith:'[/quote]',
					  placeHolder:text }

		);*/
		return false;
	}); 
	
	$("#nereikalingas_pristatymas").change(function(){
		if ($('#nereikalingas_pristatymas').attr('checked')) {
		   $("#pristatymoKaina").css("display", "none");
		   var cost=parseFloat($("#totalCost").html());
		   cost=cost-15;
		   $("#totalCost").html(cost);
		   
		}else{
			$("#pristatymoKaina").css("display", "block");
			var cost=parseFloat($("#totalCost").html());
			cost=cost+15;
			$("#totalCost").html(cost);
		}
	});
	
	$(".editComment").click(function() {
		//href = "ddd";
		id=$(this).attr("alt");
		commentMod=$("#commentSendForm #mod").val();
		
		
		$.post("user.php?mod=m_users&type=110&id="+id,
				{
				change: "true",
				action: "editComment",
				commentMod: commentMod },
				function(data)
				{
					if(data.sucess!="true"){
						alert(data.message);	
					}else{
					
						$("#commentId").val(id);
						$.markItUp(
								{     target:'#comment',
									  openWith:'',
									  closeWith:'',
									  placeHolder:data.message }

						);
					}
				}, "json");	
		return false;
	}); 	
	
	$("#placeBid").trigger("unload").unbind().click(
			function(event){		
				event.preventDefault();
				var betCredits=$('#betCredits').val();
				if ((isNumber(betCredits))&&(betCredits!='')){
					lodeMainText("Statymas,13,4,"+betCredits+".html");
				}
				//'http://localhost/nba_world/Totalizatirius_statymas,13,1,0.html'
				
			});	
	
	
	
	$("a.showNews").trigger("unload").unbind().click(
			function(event){		
				event.preventDefault();	
				lodeMainText($(this).attr("href"));
				return false;
			});	
	
	
	initForms();
	
	
	
	
	
	
	/* Cart */
	
	$("a.changeCartCount").trigger("unload").unbind().click(
			function(event){	
			//	$()
				event.preventDefault();	
				
				var id=parseInt($(this).attr("title"));
				var new_count=parseInt($('#count_'+id).val());
				
				var send=(id*1000)+new_count;
				
				//;
				//event.preventDefault();
				//var betCredits=$('#betCredits').val();
				//if ((isNumber(betCredits))&&(betCredits!='')){
				lodeMainText("Keitimas,16,17,"+send+".html");
			//	lodeMainText($(this).attr("href"));
				return false;
			});	
	
	
	/* Cart */
	
	
	
	
	
	$.post("user.php?mod=m_users&type=130&id=0",
			{
			change: "true" },
			function(data)
			{
				if(data.sucess=="true"){
					//alert ("OK");
					//	alert (data.newMessages);
					if (data.newMessages==0){
						
						$("#newMessagesTitle").css("color","");
						$("#newMessagesCount").html("0");
					}else{
						$("#newMessagesTitle").css("color","red");
						$("#newMessagesCount").html(data.newMessages);
					}
					$("#creditsCount").html(data.credits);
					$("#pointsCount").html(data.points);
				}else{
				
					
				}
			}, "json");
	
	
	
	
	
	
	
	
	
	
}

function lodeMainText(page){
	if (tb_visible==true){
		tb_remove();
	}
	
	showLoading();
	$("#context").animate({height: 'hide'},'slow',
	function(){
		hash=page;
		window.location.hash = page;  
		
		$("#context").load(page+"&plain=1",function(){
			
			thumbnailviewer.init();
			
			$("#context").animate({height: 'show'},'slow',
			function (){
				
				 advertisement_left_side_reload( $("#context").height() );
				 $("#rightSideNews").load(page+"&rside_news=1",
				function(){
					 hideLoading();
					 initLinks();
					 
				 }
				 
				 );
			}
			);
		});
		
	}
	
	);
	
	
	
}

function showLoading(){
	var tb_pathToImage = "tpl/nba_world/img/loadingAnimation.gif";
	imgLoader = new Image();// preload image
	imgLoader.src = tb_pathToImage;
	if (typeof document.body.style.maxHeight === "undefined") {//if IE 6
		$("body","html").css({height: "100%", width: "100%"});
		$("html").css("overflow","hidden");
		if (document.getElementById("TB_HideSelect") === null) {//iframe to hide select elements in ie6
			$("body").append("<iframe id='TB_HideSelect'></iframe><div id='TB_overlay'></div>");
			$("#TB_overlay").click(tb_remove);
		}
	}else{//all others
		if(document.getElementById("TB_overlay") === null){
			$("body").append("<div id='TB_overlay'></div>");
			$("#TB_overlay").click(tb_remove);
		}
	}
	
	if(tb_detectMacXFF()){
		$("#TB_overlay").addClass("TB_overlayMacFFBGHack");//use png overlay so hide flash
	}else{
		$("#TB_overlay").addClass("TB_overlayBG");//use background and opacity
	}
	
	
	
	$("body").append("<div id='TB_load'><img src='"+imgLoader.src+"' /></div>");//add loader to the page
	$('#TB_load').show();//show loader
}

function hideLoading(){
	$('#TB_overlay,#TB_HideSelect').trigger("unload").unbind().remove();
	
	$("#TB_load").remove();
	if (typeof document.body.style.maxHeight == "undefined") {//if IE 6
		$("body","html").css({height: "auto", width: "auto"});
		$("html").css("overflow","");
	}
	document.onkeydown = "";
	document.onkeyup = "";	
}

function tb_remove() {
 	$("#TB_imageOff").unbind("click");
	$("#TB_closeWindowButton").unbind("click");
	$("#TB_window").fadeOut("fast",function(){$('#TB_window,#TB_overlay,#TB_HideSelect').trigger("unload").unbind().remove();});
	$("#TB_load").remove();
	if (typeof document.body.style.maxHeight == "undefined") {//if IE 6
		$("body","html").css({height: "auto", width: "auto"});
		$("html").css("overflow","");
	}
	document.onkeydown = "";
	document.onkeyup = "";
	return false;
}

function tb_detectMacXFF() {
	  var userAgent = navigator.userAgent.toLowerCase();
	  if (userAgent.indexOf('mac') != -1 && userAgent.indexOf('firefox')!=-1) {
	    return true;
	  }
	}



var sviesTotal=15;
var sviesCurr=1;
var svieslenteInAction=0;
var svieslenteActiveTimer=0;
var svieslenteEnabledTimer=1;

function svieslenteOnTimer(){
	svieslenteActiveTimer=0;
	if (svieslenteEnabledTimer==1){
		svieslenteLoadNext();
	}
	
}

function svieslenteActivateTimer(){
	if ((svieslenteActiveTimer==0)&&(svieslenteEnabledTimer==1)){
		svieslenteActiveTimer=1;
		setTimeout("svieslenteOnTimer()",5000);
	}
	
}

function svieslenteLoadPrew(){
	if (svieslenteInAction==0){
		if (sviesCurr>1){
			sviesCurr--;	
		}else{
			sviesCurr=sviesTotal;
		}
	
		svieslenteLoad(sviesCurr);
	}
}

function svieslenteLoadNext(){
	if (svieslenteInAction==0){
		if (sviesCurr<sviesTotal){
			sviesCurr++;	
		}else{
			sviesCurr=1;
		}
		svieslenteLoad(sviesCurr);
	}
}

function svieslenteLoad(curr){
	svieslenteInAction=1;
	var img = new Image();
	var newUrl="testgd.php?id="+curr+"&t="+sviesTotal+"&c="+curr;

	$(img).load(
		function () {	 
			$('#svieslenteImg2').append(this).fadeIn(1000,
					function (){
						$('#svieslenteImg1').empty().append(img);
						$('#svieslenteImg2').hide().empty();
						$("#svieslenteStats").unbind();
						$("#svieslenteStats").click(function(){
							//alert(curr);
							lodeMainText("Statistika,15,3,"+curr+".html");
						});
						svieslenteInAction=0;
						svieslenteActivateTimer();
    				}
    		);   
    })
    .error(function () {
    })
    .attr('src', newUrl);
}






function pageload(hash) {
	 
	// hash doesn't contain the first # character.
	if(hash) {
		
		lodeMainText(hash);
		//$("#load").load(hash + ".html");
	} else {
		// start page
		//$("#load").empty();
	}
}





var hash="";

function hashCacker(){
	document.title = "NBAworld.lt";
	var currHash=window.location.hash.replace(/^#/, '');
	if (hash!=currHash){
		if (currHash.search(/.html/i)!=-1){
			lodeMainText(currHash);
		//alert(hash+"<>"+currHash);
			setTimeout("hashCacker()",100);
		}
	}
	setTimeout("hashCacker()",100);
}


function search(){
	 $("#context").animate({height: 'hide'},'slow');
	  showLoading();
	  var searchStr=$("#search").val();
		
	 $.post("user.php?plain=1&mod=news&type=10&id=0",
			{
				change: "true",
				action: "newSearch",
				mod: 'news',
				type: 10,
				id: 0,
				plain: "1",
				search:searchStr },
				function(data)
				{
					if(data.sucess!="true"){
				
					
					}else{
						lodeMainText("Paieska,3,10,0.html");
				
					}
					 
					},  "json"
					
					);
}


$(document).ready(
	function() 
 		{ 
	
		$('marquee').marquee('pointer').mouseover(function () {
            $(this).trigger('stop');
        }).mouseout(function () {
            $(this).trigger('start');
        }).mousemove(function (event) {
            if ($(this).data('drag') == true) {
                this.scrollLeft = $(this).data('scrollX') + ($(this).data('x') - event.clientX);
            }
        }).mousedown(function (event) {
            $(this).data('drag', true).data('x', event.clientX).data('scrollX', this.scrollLeft);
        }).mouseup(function () {
            $(this).data('drag', false);
        });

		
		
		addJavascript('tpl/nba_world/js/nba_formsubmit.js','head');
		
		setTimeout("hashCacker()",100);
	//	$.historyInit(pageload, "index.php");
		
		
		
		

		
		
		 advertisement_left_side_reload( $("#context").height() );
		

			
		svieslenteLoad(1);
		sviesCurr=1;
			
		svieslenteActivateTimer();
		
		
		
		
		$("#svieslenteNext").click(function(){
			svieslenteLoadNext();			
		});
		$("#svieslentePrew").click(function(){
			svieslenteLoadPrew();
		});
		$("#svieslenteFull").click(function(){
			lodeMainText("Rezultatai,7,12,0.html");
		});
		
		//$("#svieslenteStats").click(function(){
		//	lodeMainText("Statistika,7,12,0.html");
		//});
		
		
		
		$("#svieslentePlayPause").click(function(){
			if (svieslenteEnabledTimer==1){
				svieslenteEnabledTimer=0;
			}else{
				svieslenteEnabledTimer=1;	
			}
			svieslenteActivateTimer();
		});
		
		
		
		$("#search").click(function(){
			if ($(this).val()=='Paieška...'){
				$(this).val("");
			}
		}).blur(
				function(){
			if ($(this).val()==''){
				$(this).val("Paieška...");
			}
			})
		.keypress(function (e) {
				  if (e.which == 13){
					  search();
					
				  }
				
			});
		
		$("#send_news_email").click(function(){
			if ($(this).val()=='El. paštas...'){
				$(this).val("");
			}
		}).blur(
				function(){
			if ($(this).val()==''){
				$(this).val("El. paštas...");
			}
			})
		.keypress(function (e) {
				  if (e.which == 13){
					 // search();
					
				  }
				
			});
		
		
		$("#search_ico").click(function(){
			search();
		});
		
		hash = window.location.hash.substr(1);   
		
		if (hash!=''){
			lodeMainText(hash);
		}
			
		
 		}
	);