function slideGridUp(ticker) {
	var tblGridHeight = $("#box2_content1 .box2_scrollbox").height();
	tblGridHeight = tblGridHeight - 34;
	if (ticker) { $("#box2_content1_scrollboxInner tbody tr[rel='"+ticker+"']").remove(); }
	if ( $("#box2_content1_scrollboxInner tbody tr").length <= 8 ) {
		$("#box2_content1_arrows").hide();
		$("#box2_content1 .box2_scrollboxInner").css("height","auto");
		$("#box2_content1 .box2_scrollbox").animate({ height: tblGridHeight }, 100 );
	}
	if ( $("#box2_content1_scrollboxInner tbody tr").length >= 9 ) {
	    if ( $("#box2_content1_scrollboxInner tbody tr").length == 9 ) { $("#box2_content1_arrows").hide(); }
	    if ( scrollAmount[0] != 0 ) {
			scrollAmount[0] = scrollAmount[0]-34;
			$("#box2_content1_scrollboxInner table").animate({
				marginTop: "-"+scrollAmount[0]+"px"
			}, 100 );
		}
	}
	if ( $("#box2_content1_scrollboxInner tbody tr").length == 0 ) {
		$("#box2_nostock").show();
		$("#box2_stock").hide();
	}
	$("#box2_content1_scrollboxInner table").trigger("update");
}

function insertGridTicker(grHeight,tcLoc) {  
    var columns = new Array();
  	for ( i=4; ($(".box2_tbl_head th").length-1) > i; i++ ) {
  	  columns[i-4] = $(".box2_tbl_head th:eq("+i+")").attr("rel");
  	}
  	columnsStr = "star|name|last|button|precent|analysis|"+columns.join("|");
    $("#box2_content1 .box2_scrollbox").animate({ height: grHeight }, 100, function(){
        $.getJSON(tcLoc,{data: columnsStr}, function(data){
          var imagesScore = (data.star)?'<img src="'+urlRoot+'images/ICONstar'+data.star.substr(0,1)+'.gif" alt="" />':'';
          if (data.analysis=="0") {
              var analLinkOff = "class='off'";
          } else {
              var analLinkOff = "href="+urlRoot+"analiza_dionica/"+data.ticker+" ";
          }
          $("#box2_content1_scrollboxInner tbody").prepend('<tr rel="'+data.ticker+'"><td class="mark"><img src="'+urlRoot+'images/ICONclose.gif" alt="izbriši"></td><td class="name"><span>'+data.ticker+'</span>'+imagesScore+'<div title="'+data.name+'">'+data.name+'</div></td><td class="lastprice">'+data.last+'</td><td class="precent '+data.button+'"><img src="'+urlRoot+'images/ICON'+data.button+'.gif" alt="pad" height="15" width="14">('+data.precent+'%)</td><td class="alg_right" rel="+columns[0]+">'+data[columns[0]]+'</td><td class="alg_right" rel="'+columns[1]+'">'+data[columns[1]]+'</td><td class="alg_right" rel="+columns[2]+">'+data[columns[2]]+'</td><td class="alg_right" rel="+columns[3]+">'+data[columns[3]]+'</td><td class="alg_right '+((scrRes=="small")?"noSmall":"")+'" rel="+columns[4]+">'+data[columns[4]]+'</td><td class="alg_right" rel="+columns[5]+">'+data[columns[5]]+'</td><td class="alg_center last">'+data.sector+'</td><td class="graph"><a href="'+urlRoot+'grafovi/'+data.ticker+'" title="Graf dionice: '+data.name+'"><img src="'+urlRoot+'images/ICONgraph_type1.gif" alt="Graf dionice: '+data.name+'"></a><a '+analLinkOff+' title="Analiza dionice: '+data.name+'"><img src="'+urlRoot+'images/ICONgraph_type2.gif" alt="Analiza dionice: '+data.name+'"></a></td></tr>');    
          $("#box2_content1_arrows .down").removeClass("off");
          $("#box2_content1_scrollboxInner .mark").unbind("click");
          box2grid();
    			$("#box2_content1_scrollboxInner table").trigger("update");
    			$("#box2_content1_scrollboxInner table [title]").tooltip({ 
              track: false, 
              delay: 0, 
              showURL: false
          });    			
        });		
  	});
}

function saveNewTicker(tblGridHeight,tickerLocation,ticker) {
  $.get(urlRoot+"index.php?page=updateUserApartStocks&action=insert&stock="+ticker, function(data){
    insertGridTicker(tblGridHeight,tickerLocation);
	});
}


function box1grid() {
	$("#box1_content1 .check .jquery-checkbox-box").click(function(){
	 if ( $(this).hasClass("jquery-checkbox-checked") ) {
			var ticker = $(this).parents("tr").attr("rel");
			$.get(urlRoot+"index.php?page=updateUserApartStocks&action=delete&stock="+ticker, function(data){
  	    slideGridUp(ticker);
  		});
		}
		else {
		    var tickerLocation = urlRoot+"index.php?page=stocksApartBox&ticker="+$(this).parents("tr").attr("rel");
		    var ticker = $(this).parents("tr").attr("rel");
		    if ( $("#box2_content1").is(":hidden") ) {
		        $("#box2 .nav li").removeClass('on');
		        $("#box2_link1").parents("li").addClass("on");
    				$("#box2_content2").hide();
    				$("#box2_content3").hide();
    				$("#box2_content4").hide();
    				var boxHeight = $("#box2_content1").height();
    				$("#box2 .inner").animate({ height: boxHeight}, 600, function(){
    				    $("#box2 .inner").css("height","auto");
    				    $("#box2 .inner .ajax_preloader").remove();
    				    $("#box2_content1").show();
    					var tblGridHeight = $("#box2 .box2_scrollbox").height();
    			    	if ( $("#box2_content1_scrollboxInner tbody tr").length == 0 ) {
    						$("#box2_nostock").hide();
    						$("#box2_stock").show();
    						saveNewTicker(34,tickerLocation,ticker);
    					} else {
    					    tblGridHeight = tblGridHeight + 34;
    					    saveNewTicker(tblGridHeight,tickerLocation,ticker);
    					    if ( $("#box2_content1_scrollboxInner tbody tr").length >= 9 ) {
    			                $("#box2_content1_arrows").show();
    			                $("#box2_content1 .box2_scrollboxInner").css("height","307px");
    						}
    					}
    				});
    			} else {
              var tblGridHeight = $("#box2 .box2_scrollbox").height();
              if ( $("#box2_content1_scrollboxInner tbody tr").length == 0 ) {
                $("#box2_nostock").hide();
                $("#box2_stock").show();
                saveNewTicker(34,tickerLocation,ticker);
              } else {
                if ( $("#box2_content1_scrollboxInner tbody tr").length >= 9 ) {
                  $("#box2_content1_arrows").show();
                  $("#box2_content1 .box2_scrollboxInner").css("height","307px");
                  saveNewTicker(tblGridHeight,tickerLocation,ticker);
                } else {
                  tblGridHeight = tblGridHeight + 34;
                  saveNewTicker(tblGridHeight,tickerLocation,ticker);
                }
              }
    			}
		  }
	});
}
/* REMOVE STOCK */
function box2grid() {
  over_hover ("#box2_content1_scrollboxInner tbody tr");
	box2click ("content1");
	$("#box2_content1_scrollboxInner .mark").click(function(){
      if ( $(this).parents("tr").is(".active") ) {
        $("#box3 .inner > div object").remove();
        $("#box3 .inner > div").html("");
        $("#box3_content1").show();
        $("#box3_content2").hide();
        $("#box3_content3").hide();
        $("#box3_content1").html("<span class='noneSelected'>Trenutno nije niti jedna dionica selektirana</span>");
        $("#box4 .inner > div").html("");
        $("#box4_content1").show();
        $("#box4_content2").hide();
        $("#box4_content3").hide();
        $("#box4_content4").hide();
        $("#box4_content1").html("<span class='noneSelected'>Trenutno nije niti jedna dionica selektirana</span>");
      }	
	    var ticker = $(this).parents("tr").attr("rel");
	    $.get(urlRoot+"index.php?page=updateUserApartStocks&action=delete&stock="+ticker, function(data){
  	    $("#box1_content1 tr[rel='"+ticker+"']").toggleCheckboxes();
        slideGridUp(ticker);
  		});
	});
}


