$(document).ready(function(){ 
    $("ul#mainnav").superfish({
        autoArrows:  false
    });
    equalHeight();
    $('#carlist > ul').jcarousel({scroll:1});
    $('.lightbox').fancybox();
    $('#tab-cars > a').click(function() { return false; })
    $('#tab-cars > ul > li > a').click(function() { return false; });
    scrollToUp();
    if ($('#carlist').length > 0) {
        carList();
    }
});

function scrollToUp() {
    $('#goup').click(function() {
        $.scrollTo('#carlist', 500);
        return false;
    })
}

function equalHeight() {
    $('#carlist .caritem')/* .css({'height':''}) */.setEqualHeight();
    $('#reflist > li').setEqualHeight();
}


function carList() {
    //$('.jcarousel-container').append('<div id="loading"><img src="gfx/ajax_load.gif" /></div>');
    //checkUrl();
    $('#carlist a').click(function() {
        loadPage($(this).attr('href'));
        $.scrollTo('#content', 500);
        return false;
    });
    //setInterval("checkUrl()", 250);
}

/*
var lastUrl = '';

function checkUrl(hash) {
    if (!hash) hash = window.location.hash;
    if (hash != lastUrl) {
        lastUrl = hash;
        loadPage(hash);
    }
}
*/

function loadPage(url) {
    url = url.split('.');
    url = url[0];
    $.ajax({
        type: 'POST',
        url: 'loadproduct.php',
        data: 'link=' + url,
        success: function(msg) {
            if (parseInt(msg) != 0) {
                $('#content').html(msg);
                $.scrollTo('#content', 500);
                scrollToUp();
                $('.lightbox').fancybox();
            }
        }
    })
}

jQuery.fn.setEqualHeight=function(o) {
    var maxHeight=0;
    var maxElement=null;
    	jQuery(this).each(function(i) {
      		if((jQuery(this).height()+parseInt(jQuery(this).css("padding-bottom"))+parseInt(jQuery(this).css("padding-top")))>maxHeight) {
    		maxHeight=jQuery(this).height()+parseInt(jQuery(this).css("padding-top"))+parseInt(jQuery(this).css("padding-bottom"));
    		maxElement=this;
    	}
    });
    //jQuery(this).not($(maxElement)).each(function() {$(this).height(maxHeight-parseInt(jQuery(this).css("padding-top"))-parseInt(jQuery(this).css("padding-bottom")))})
    jQuery(this).each(function() {$(this).height(maxHeight-parseInt(jQuery(this).css("padding-top"))-parseInt(jQuery(this).css("padding-bottom")))})
}
