$(document).ready( function()
{
	    var obl = $('#obl');
        var gr = $('#g');
		var kv = $('#kv');
		
		var kvartal = $('#kv_div');
		var grad = $('#gr_div');
		var rub = $('#rub');
		//var to = $('#tip_obiava_id');
        
		//kvartal.hide();

		$('.vip_pad').hide();
		
		if (gr.val() == 22)
			$('.vip_pad').show();
		
		if (rub.val() > 0)
			$('.vip_pad').show();
		
		obl.change(function(){  
			if ($(this).val() <=28 && $(this).val() > 0) 
			{
				kvartal.show();
				grad.show();
			}
			else { 
			kvartal.hide();
			grad.hide();
			kv.children().remove();	
			gr.children().remove();
			}
		});
		
		
		
		
		gr.change(function(){  
			if ($(this).val() <=28 && $(this).val() > 0) 
			{
				kvartal.show();
				$('.vip_pad').show();
			}
			else { 
			kvartal.hide();
			kv.children().remove();
			if (rub.val() == 0)
				$('.vip_pad').hide();
			}
		});
		
		gr.selectChain({
		target: kv,
		url: 'select-chain_srch.php',
		data: { ajax: true }
		});
		
		obl.selectChain({
            target: gr,
            url: 'select-chain_srch.php',
            data: { ajax: true }
        });
		


		function in_array(string, array)
		{
		  for (i = 0; i < array.length; i++)
		   {
		      if(array[i] == string)
		     {
		        return true;
		      }
		  }
		return false;
		}
		
		


		
		rub.change(function(){  

				if (in_array(rub.val(), tipObiavi)){
    				$("#t_o").load("php/tip_obiava_srch/to_"+rub.val()+".php");}
    		   else $("#t_o").load("php/tip_obiava_srch/to_1.php");
			
			
			
			if ($(this).val() > 0 && $(this).val() < 700) 
			{
				$('.vip_pad').show();			}
			else
			{
				if(gr.val() == 0 || gr.val() > 28)
				$('.vip_pad').hide();

			}
			
			});
	

		function log(event, data, formatted) {
			$("<li>").html( !data ? "No match!" : "Selected: " + formatted).appendTo("#result");
		}
		
		function formatItem(row) {
			return "<div style=\"float:left\">" + row[0] + "</div>" + "<div style=\"float:right\">" + row[1] + "</div>";
		}
		function formatResult(row) {
			return row[0].replace(/(<.+?>)/gi, '');
		}
		
		$("#srch").autocomplete('search.php', {
			width: 245,
			cacheLength: 50,
			minChars: 2,
			matchContains: "word",
			autoFill: false,
			selectFirst: false,
			formatItem: formatItem,
			formatResult: formatResult,
			scroll: true,
			scrollHeight: 360
			
		});
		
		
		
		var topH = $("#topOffer").height();
		var topXY = $("#topOffer").offset()
		var topY = topXY.top - (topH+20);
		var footXY = $(".footline").offset();
		var bottomY = footXY.top - 2.35*topH;
		$('#topOffer').makeFloat({x: 7, y: -(topH+20), minY: topY, maxY: bottomY});
		
		$(".clickthrough").click(function() {
			theId = $(".clickthrough").attr("id");
			$.get("clickThroughTargovec.php?vars="+theId);
			//alert(theId);
		    		    
		});
		
		

		$("#subscribeBtn").click(function() {

			if(clicked)
		    {
		    	$("#subscribeBtn").attr('value', 'Затвори абонаментa!');
		    	$("#subscribe").show();
		    	clicked = false;
		    }
		    else if(!clicked)
		    {
			    $("#subscribeBtn").attr('value', 'Абонирай се!');
			    $("#subscribe").hide();
			    clicked = true;
		    }
		    
 
		}); 



		
});
