
function showBrand(evt){
    jQuery(this).children(".logo").fadeOut();
    jQuery(this).children(".thumb").fadeIn();
}

function hideBrand(evt){
    jQuery(this).children(".logo").fadeIn();
    jQuery(this).children(".thumb").fadeOut();
}

function pageTo(page_num){
    var pages = jQuery('.find-result-list');
    if (page_num < pages.length && page_num >= 0){
        pages.hide();
        pages.eq(page_num).show().pngFix();
        pages.removeClass('visible');
        pages.eq(page_num).addClass('visible');
        var top_pagelinks = jQuery('#pagination-top li');
        top_pagelinks.removeClass('active');
        top_pagelinks.eq(page_num).addClass('active');
        var bottom_pagelinks = jQuery('#pagination-bottom li');
        bottom_pagelinks.removeClass('active');
        bottom_pagelinks.eq(page_num).addClass('active');
    }
}

function closeLightbox(){
    jQuery('#map').hide().appendTo('body');
    jQuery('#modal, #overlay').remove();
}

function showLightbox(evt){
    //Get the screen height and width
    var maskHeight = jQuery(document).height();
    var maskWidth = jQuery(window).width();

    //Set heigth and width to mask to fill up the whole screen
    jQuery('body').append('<div id="overlay"></div>');
    var overlay = jQuery('#overlay').css({
            width: maskWidth,
            height: maskHeight,
            opacity: 0.5,
            position: 'absolute',
            left : 0,
            top : 0,
            backgroundColor : 'black',
            zIndex : 9990
            });

    //Get the window height and width  
    var winH = jQuery(window).height();  
    var winW = jQuery(window).width();  
            
    jQuery('body').append('<div id="modal"></div>');
    jQuery('#modal').append(jQuery('#map'));
    jQuery('#modal').css({
            position        :   'absolute',
            left            :   '50%',
            top             :   50,
            zIndex          :   9995,
            marginLeft      :   (jQuery('#map').width()/2) * -1
    });
    jQuery('#map').show().pngFix();
    overlay.click(function(evt){
        closeLightbox();
    });
    return false;
}

jQuery(".find-result-container").ready(function() {
    jQuery(document).pngFix();
    jQuery('.find-result-item').hover(showBrand, hideBrand);
    jQuery('.find-result-item').click(function(){
        window.location.href = jQuery(this).find('a.go-view-details').attr('href');
        });
    jQuery('.go-page').click(function(evt){
        var parts = jQuery(evt.target).attr('id').split('-');
        var page_num = parts[parts.length-1];
        pageTo(page_num-1);
        return false;
    });
    jQuery('.go-next').click(function(evt){
        var pages = jQuery('.find-result-list');
        var page_num = pages.index(jQuery('.find-result-list.visible')) + 1;
        pageTo(page_num);
        return false;
    });
    jQuery('.go-prev').click(function(evt){
        var pages = jQuery('.find-result-list');
        var page_num = pages.index(jQuery('.find-result-list.visible')) - 1;
        pageTo(page_num);
        return false;
    });
    jQuery('.go-map').click(showLightbox);
    jQuery('#mask, #map-controls-close').click(function(){
        closeLightbox();
    });
});

jQuery(document).ready(function(){
	var init = false;
	var current_image = 1;
	var next_image = 1;
	var num_images = jQuery('#showcase-images li').size();
	
	// set option 1 to selected state
	jQuery('#showcase-options a#option1').css("background-position", "0 -32px");

	function selected(current_image, next_image) {
		jQuery('#showcase-options a#option' + current_image).css("background-position", "");
		jQuery('#showcase-options a#option' + next_image).css("background-position", "0 -32px");
	}
	
	if(num_images == 1) {
		// remove options
		jQuery('#showcase-options, #showcase-bg').remove();
	} else {
		for(var i = 0; i <= 3; i++) {
			if(i > num_images) {
				jQuery('#showcase-options li:eq(' + i + ')').remove();
			}
		}
	}
	
	jQuery('#showcase-options a').click(function(){
		var option = jQuery(this).attr('id').replace(/option/, '');
		
		if(option == 'prev') {
			next_image = Number(next_image) - 1;
		} else if(option == 'next') {
			next_image = Number(next_image) + 1;
		} else {
			next_image = option;
		}
		
		// bound check
		if(next_image < 1) {
			next_image = 1;
		} else if(next_image > num_images) {
			next_image = num_images;
		}
		
		if(current_image != next_image) {
			if(!init) {
				jQuery('#showcase-options a.selected').attr("class", "");
				init = true;
			}
			
			jQuery('#showcase-images #image' + current_image + ' img').fadeOut("slow");
			
			// show image
			jQuery('#showcase-images #image' + next_image + ' img').fadeIn("slow");
			
			selected(current_image, next_image);
			
			current_image = next_image;
		}
		
		return false;
	});
});