jQuery.fn.extend({

    // returns highest DOM object of an array
    highest: function () {

        var $t = this;

        if ($t.length > 1) {

            var $h = null;

            $t.each(function () {

                var $k = jQuery(this);

                if ($h) {

                    if ($k.outerHeight() > $h.outerHeight()) {
                        $h = $k;
                    }

                } else {

                    $h = $k;

                }

            });

            return $h;

        } else {

            return $t;

        }

    }

});

(function ($) {
    
    $(function () {
        
    default_q = 'Pagal raktinį žodį';
    default_city = jQuery("#city option:first").val()
    default_theme = jQuery("#theme option:first").val()
    default_age = jQuery("#age option:first").val()
    default_price = jQuery("#price option:first").val()
    
    $(".list-00").each(function () {
        
        var $t = $(this);
        
        if ($t.find('.sticky').length) {
            
            $heighest = $t.find('h2').highest();
            $t.find('.h2').css('height', $heighest.height());
            $t.find('.stats').css('marginTop', $heighest.height());
            
            $heighest = $t.find('li').highest();
            $t.find('li').css('height', $heighest.height());
            
        } else {
            
            $heighest = $t.find('h2').highest();
            $t.find('h2').css('height', $heighest.height() + 4);
            
            $heighest = $t.find('li').highest();
            
            $t.find('li').css('height', $heighest.height());
            
        }
        
    })
						
    jQuery("#q").click( function () {
		
        if (jQuery(this).val() == default_q) {
            jQuery(this).val('');
        }
		
    });
	
    jQuery("#q").blur( function () {
		
        if (jQuery(this).val() == '') {
            jQuery(this).val(default_q);
        }
		
    });
						   
    jQuery("#btnShowAllData").toggle(function () {

        jQuery(this).addClass("none");
        jQuery("#table-all-data").show();
										 
    }, function () {
		
        jQuery(this).removeClass("none");
        jQuery("#table-all-data").hide();
		
    });
	
    jQuery(".open-popup").click( function () {
										   
        window.open(jQuery(this).attr("href"), 'rules', 'width=600,height=500');
		
        return false;
										   
    });
	
    jQuery("#commentForm").submit( function () {
		
        if (jQuery("#agree:checked").length == 0) {
			
            alert('Norėdami komentuoti, turite sutikti ir laikytis komentavimo taisyklių.')
			
            return false;
			
        }
										
    });
	
    jQuery("#aMoreComments").toggle( function () {

        jQuery("#comments_box LI.hide").removeClass("hide").addClass("block");

        jQuery(this).html("Mažiau...");
											   
    }, function () {
		
        jQuery("#comments_box LI.block").removeClass("block").addClass("hide");
		
        jQuery(this).html("Daugiau...");
		
    });

    jQuery("#aMorePopular").toggle( function () {
											  
        jQuery("#week_popular LI.hide").addClass("block");

        jQuery(this).html("Mažiau...");
											  
    }, function () {
	
        jQuery("#week_popular LI.block").removeClass("block");
		
        jQuery(this).html("Daugiau...");
		
    });
	
    jQuery("#foto_gallery a").click( function () {
											   
        var img = new Image();
		
        jQuery(img).load( function () {
									
            jQuery("#photo_cont").css( {
                height: jQuery("#photo_cont").height(),
                width: jQuery("#photo_cont").width()
            });
			
            if (img_h < jQuery("#photo_cont").height()) img_h = jQuery("#photo_cont").height();
			
            jQuery("#photo_cont").animate( {
                width: img_w,
                height: img_h
            }, 400, function () {
																						   
                jQuery("#photo_cont img").before("<img src=\"\" alt=\"\" style=\"position: absolute; display: none\" />");
	
                jQuery("#photo_cont img:first").attr('src', img.src);
				
                jQuery("#photo_cont img:first").fadeIn("fast", function () {
																			

																			
                    });
				
                jQuery("#photo_cont img:last").remove();

            });
									
        });

        var imgsize = String(jQuery(this).attr('img')).split('-');
        imgsize = imgsize[imgsize.length - 1];
		
        imgsize = imgsize.split('.', 1);
        imgsize = imgsize[0];
		
        imgsize = imgsize.split('x');
		
        var img_w = imgsize[0];
        var img_h = imgsize[1];
		
        var guid_base = jQuery("#images_dir").val();

        img.src = guid_base + jQuery(this).attr('img');
		
		
    });
	
    jQuery(".iconClick").click( function () {
										  
        jQuery(this).next().click();
										  
    });
	
    jQuery(".list-00 > li").click( function () {

        if (typeof event != "undefined") {

            var target = jQuery(event.target);
            var href = jQuery(target).attr("href");
		
            if (href != undefined) return true;
			
        } else {
			
            if (jQuery(this).attr("href") != undefined) return true;
			
        }
		
        document.location = jQuery(this).children("div").children("A").attr("href");
												
    });
	
	
    jQuery(".list-00 > li").hover( function () {

        window.status = jQuery(this).children("div").children("A").attr("href");
												
    }, function () {
        window.status = '';
    } );

    jQuery(".form-search").submit( function () {

        var url = [];

        if (jQuery("#q").val() != default_q) {
            url.push('q=' + jQuery("#s").val());
        } else {
            url.push('q=search');
        }

        if (jQuery("#city").val() != default_city) {
            url.push('city=' + jQuery("#city").val());
        }

        if (jQuery("#theme").val() != default_theme) {
            url.push('theme=' + jQuery("#theme").val());
        }

        if (jQuery("#age").val() != default_age) {
            url.push('age=' + jQuery("#age").val());
        }

        if (jQuery("#price").val() != default_price) {
            url.push('price=' + jQuery("#price").val());
        }

        if ((url.length == 2) && (jQuery("#city").val() != default_city)) {
            if (jQuery("#q").val() == default_q) {
                //- tik miestas
                document.location = '/bureliai-' + jQuery("#city").val();
            } else {
                //- tik miestas ir paieska
                document.location = '/bureliai-' + jQuery("#city").val() + '/' + jQuery("#s").val();
            }
            return false;
        }

        document.location = jQuery(this).attr('action') + '?' + url.join('&');

        return false;

    });
	
	
    });
    
})(jQuery)
    


function showMiestai() {
	
    jQuery("#city").hide();
    jQuery("#selectCity").show()
	
    return false;
	
}

function hideMiestai() {
	
    jQuery("#selectCity").hide()
    jQuery("#city").show();
	
    return false;
	
}

function showTipai() {
	
    jQuery("#type").hide();
    jQuery("#selectType").show()
	
    return false;
	
}

function hideTipai() {
	
    jQuery("#selectType").hide()
    jQuery("#type").show();
	
    return false;
	
}

