$(document).ready(function(){
	//auto-resize ad wrapper
	counter = $(".img-wrapper li").size();
	wdth = counter * $(".img-wrapper li").outerWidth();
	$(".img-wrapper").css({"width":wdth});


//	$("select").replaceSelect();
//	$(".img-wrapper a").tooltip();
	$(".img-wrapper a").hover(
		function(){
			hepek = $(".tooltip-hover");
			pozicija = $(this).offset();
			content = '<img src="images/triangle2.png" alt="" class="triangle2" />'+$(this).siblings("div").html();
			hepek.html(content);
			
			$(this).mousemove(function(e){		
					var x = e.clientX - (hepek.outerWidth()/2);
					var y = e.clientY - (hepek.outerHeight()/2);
					hepek.css({
						"position":"fixed",
						"top": y-70,
						"left": x-5
					});
			});
			hepek.show();
		},function(){
			$(".tooltip-hover").hide();
		}
	);
	
});

jQuery.fn.replaceSelect = function(x) {

	$(this).each(function(){
		$(this).wrap('<span class="selectWrapper" />');
		$(this).hide();
		//kupi klasu
		var klasa=$(this).attr("class");
		//ukoliko ima id, dodaje listi id, ukoliko ima klasu, dodaje klasu
		if($(this).attr('id')){
			id=$(this).attr("id");
			$(this).after('<span id="'+id+'-select" class="replaced '+klasa+'"><span></span>'+$(this).val()+'</span><div class="ddholder"><ul class="repDD" id="'+id+'-list"></ul></div>');
		}else{
			$(this).after('<span  class="replaced '+klasa+'"><span></span>'+$(this).val()+'</span><div class="ddholder"><div><ul class="repDD" style="z-index:9999"></ul></div></div>');
		}
		//set replacing element's value	
		$(this).siblings("span").html('<span></span>'+$(this).val());
		$(this).find("*").each(function(){
			if($(this).is("optgroup")){
				$(this).parents("select").siblings("div").find(".repDD").append('<li class="optgrp">'+$(this).attr("label")+'</li>');
			}else{
				if($(this).parent().is("optgroup")){$(this).parents("select").siblings("div").find(".repDD").append('<li class="grouped">'+$(this).html()+'</li>');}else{
				$(this).parents("select").siblings("div").find(".repDD").append('<li>'+$(this).html()+'</li>');}
			}
		});
		//calculate paddings
		
		
		w = $(this).siblings("span.replaced").outerWidth();
		h = $(this).siblings("span.replaced").outerHeight();

					
		$(this).parent(".selectWrapper").css({'position':'relative',"width":w, "height":h});
	});			
		
	$(".replaced").click(function(e){
		$(".ddholder").fadeOut(x);
		
		lista = $(this).siblings("div.ddholder");
		if(lista.css("display") == 'none'){
				lista.fadeIn(x);
		}else{
			lista.fadeOut(x);
			lista.siblings("span").css({"z-index":"1", "position":"relative"});
		}
		mouse_is_inside = '';

		$(lista).hover(function(){ 
	        mouse_is_inside=true; 
	    }, function(){ 
	        mouse_is_inside=false;
	    });
	
	    $("body").click(function(){ 
	        if(! mouse_is_inside){$(lista).fadeOut(x);
		//lista.siblings("span").css({"z-index":"1", "position":"relative"});
			};
	    });		    
		e.stopPropagation();
	});			
	
	$(".repDD li").live('click',function(e){
		if($(this).attr("class") == "optgrp"){return false}
		$(this).parents(".selectWrapper").find("select").val($(this).html());
		$(this).parents(".selectWrapper").find("span.replaced").html('<span></span>'+$(this).html());
		$(this).parents(".selectWrapper").find(".ddholder").fadeOut(x);
		e.stopPropagation();
		
	});			
 	if($(".repDD li").css({"display":"block"})){
	    $(document).keypress(function(e){
	    	if(e.keyCode == 27){
	    			lista.fadeOut(x);
	    	}else{
	    	}
		});
	}else{
		return false
	}
};


jQuery.fn.tooltip = function(){	
	if(($(this).css("display"))=='none'){
		$(".tooltip-hover").hide();
	}
	$(this).mouseover(function(){
		var tooltip = $(this).siblings(".tooltip-data").html();
		$(".tooltip-hover").append(tooltip);
		$(".tooltip-hover").fadeIn(100);
		$(document).mousemove(function(e){		
				var x = e.clientX;
				var y = e.clientY;
				$(".tooltip-hover").css({
					"top": y-30,
					"left": x
				});
		});
	});
	
	$(this).mouseout(function(){
		$(".tooltip-hover").fadeOut(100);
		
	});
	
	$(document).ready(function(){
		$(".sidebar").masonry({columnWidth: 100 });
		$(".list-area").masonry({columnWidth: 250, singleMode: true, animate: true });
		$(".list-area dl").hover(function(){
			$(this).addClass("box-shadow");
		}, function(){ $(this).removeClass("box-shadow")});
		$(".listing").hover(function(){
			$(this).addClass("box-shadow");
		}, function(){ $(this).removeClass("box-shadow")});	
					  $('.boxy').boxy();		
	});	

};



