/*
* immediateSiblings 1.0.0 (2008-07-14)
*
* Copyright (c) 2006,2007 Jonathan Sharp (http://jdsharp.us)
* Dual licensed under the MIT (MIT-LICENSE.txt)
* and GPL (GPL-LICENSE.txt) licenses.
*
* http://jdsharp.us/
*
* Built upon jQuery 1.2.6 (http://jquery.com)
*/
$.fn.immediateSiblings = function(selector) {
var siblings = [];
if ( this.length > 0 ) {
this.each(function() {
var elm = $(this);
while ( elm.prev().is( selector ) ) {
elm = elm.prev();
siblings.unshift( elm[0] );
}
siblings.push( this );
var elm = $(this);
while ( elm.next().is( selector ) ) {
elm = elm.next();
siblings.push( elm[0] );
}
});
}
return this.pushStack( $.unique( siblings ) );
};

$(document).ready(function()
{
	$(window).bind("resize", resizeWindow);
});

function resizeWindow( e, cl ) {
    var newWindowHeight = $(window).height();
	var resultYPos = $("#results").offset().top;
	if(cl){
	resultYPos = $("#facilities").offset().top + 35;
	} else {
    resultYPos = $("#results").offset().top;
	}
    var panelHeight = $("#panel").height() + 13;
    
    // the safari cant understand the div height... (I do not know why)
    // so better we do static value
    
	// set map height
    $("#map").css("height", StoreMap.map_height);
	
    $("#results").css("height", (parseInt(newWindowHeight) - parseInt(resultYPos)) + "px");
    //$("#logocontrol").css("left", "350px");
    var totalResultsHeight = 0;
            
    var newResultsHeight = panelHeight - resultYPos;
    var container = $("#result-container");
    
    if (StoreMap.is_direct_mode)
    {
    	container = $("#direct-container");
    }
    
    $(".result", container).each(
        function(){
			totalResultsHeight += $(this).height() + 3;
        }
    );
    
    if(newResultsHeight > totalResultsHeight){
        container.css({'overflow' : 'hidden'});
        //$('#result-container').css({'height' : totalResultsHeight + 'px'});
        container.animate({ height: panelHeight+'px' }, 350, 'easeOutQuad');	
    } else {
        container.css({'overflow' : 'auto'});
        //$('#result-container').css({'height' : newResultsHeight + 'px'});
        container.animate({ height: newResultsHeight+'px' }, 350, 'easeOutQuad');	
    }
    //$("#debug").append("resultYPos : " + totalResultsHeight);
}

function make_up_store_result(store, is_hidden)
{
	var result_name = $(".result-name a", store);
	var result_button = $(".result-button", store);
    var result_options = $(".result-options", store);
    
    // set the click handler when dealer name is clicked
    result_name.click(function()
    {
    	// get the store object
    	var store = $(this).parents(".result");
    	
    	// open the store and close the other
    	store_result_focus(store, function()
    	{
    		var first_option = $(".option:first", store);
    		var nid = first_option.attr("nid");
    		var type = first_option.attr("type");
    		
			var marker = StoreMap.substores[nid].marker;
    		
			// open the store panel marker
			if (marker)
			{
				GEvent.trigger(marker, "click");
			}
			
    		// then automatic select the first option that they have
    		store_open_option(store, type);
    	});
    });
    
    // set the click handler when open/close button selected
    result_button.click(function()
    {
    	// get the store object
    	var store = $(this).parents(".result");
        store_result_toggle(store);
    });
    
    // iterate all option that registered into the store
    $(".option", result_options).each(function()
    {
    	// when got click open the option
    	$(this).click(function()
    	{
    		var nid = $(this).attr("nid");
    		var type = $(this).attr("type");
    		var marker = StoreMap.substores[nid].marker;
    		
			// open the store panel marker
			if (marker)
			{
				GEvent.trigger(marker, "click");
			}
			
    		// open the store panel in the left
    		store_open_option($(this).parents(".result"), type);
    	});
    });
    
    result_options.css("display", "block");
    
    // check if the store div element should be hidden or not
    // if not hidden it means the top of the list
    if (is_hidden)
    {
         result_options.css("height", "0px");
         result_button.css({"background-position":"top left"})
    }
    else
    {
        store.addClass("active");
        result_button.css({"background-position":"bottom left"})
    }
	
	$(".result-info .info", store).each(function()
	{
		var nid = $(this).attr("nid");
    	var type = $(this).attr("type");
		
		$(".btn-getdirection", $(this)).click(function()
		{
			StoreMap.selected_substore = StoreMap.substores[nid];
		});
		
		$(".btn-getdirection", $(this)).colorbox(
		{
			width: StoreMap.direct_panel_width,
			height: StoreMap.direct_panel_height,
			inline: true,
			href: "#direct-content"
		});
	});
}

// adjust the height of specified store option
function store_adjust_result_option(options, callback)
{
	// get current height value
	var current_height = options.css("height");
	
	// remove the height so that we could measure what is the auto height value
	options.css({"height" : ""});

	// get the auto height value
	var store_height = options.height() + "px";
	
	// put back the height to current
	options.css({"height" : current_height});
	
	// animate it
	options.animate({height: store_height}, 350, "easeOutQuad", function()
	{
		// check if the callback is defined
		if (callback)
		{
			// run it
			callback();
		}
	});
}

// handy function to scroll specified store to the top of the result
function store_animate_to_top(store, callback)
{
	var store_container = $("#result-container");
    // get the first div store
    var first_store = $("#result-container .result:first");
    var first_store_offset = 0;
    // find the div that we interested
	var target_store = store;
	var target_store_offset = 0;
    // find the current active option
    var active_store = $("#result-container .result.active:first");
    var active_store_offset = 0;
    var active_store_options = $(".result-options", active_store);
    var active_store_options_height = 0;
    // adjustment size to be determined whether the target store to be opened is in the upper or lower of the current active store
    var adjustment = 0;
    
    if (first_store.length > 0)
    {
        first_store_offset = first_store.offset().top;
    }
    
    if (target_store.length > 0)
    {
        target_store_offset = target_store.offset().top;
    }
    
    if (active_store.length > 0)
    {
        active_store_offset = active_store.offset().top;
    }
    
    if (active_store_options.length > 0)
    {
        active_store_options_height = active_store_options.height();
    }
    
    if (active_store_offset < target_store_offset)
    {
        adjustment = active_store_options_height;
    }

    // check if there is store in the result
    if (target_store.length > 0)
    {
        store_container.animate(
        {
            scrollTop: (target_store_offset - first_store_offset) - adjustment
        }, 1000, null,
        function()
        {
        	// check if the callback is defined
			if (callback)
			{
				// run it
				callback();
			}
        });
    }
}

// handler to open the store result panel
// it checks whether the result is already open or not
function store_result_open(store, callback)
{
	var button = $(".result-button", store);
	var options = store.find(".result-options");
	var options_height = options.height();
	
	if (options_height == 0)
	{
		store_animate_to_top(store, function()
		{
			store.addClass("active");
			button.css({"background-position":"bottom left"});
				
			store_adjust_result_option(options, callback);
		});
	}
	else
	{
		// check if the callback is defined
		if (callback)
		{
			// run it
			callback();
		}
	}
}

// handler to close the store result panel
// it checks whether the result is already close or not
function store_result_close(store, callback)
{
	var button = $(".result-button", store);
	var options = store.find(".result-options");
	var options_height = options.height();

	if (options_height > 0)
	{
		button.css({"background-position":"0px 0px"});
		store.removeClass("active");
		options.animate({ height:"0px"}, 350, "easeOutQuad", function()
		{
			// check if the callback is defined
			if (callback)
			{
				// run it
				callback();
			}
		});
	}
}

// handler to open/close specified store
function store_result_toggle(store, callback)
{
	var target_store = store.parent().find(".result.active:first");
	var target_store_nid = target_store.attr("name");
	var store_nid = store.attr("name");
		
	if (target_store_nid != store_nid)
	{
		if (target_store_nid != null)
		{
			store_result_close(target_store, function()
			{
				store_result_open(store, callback);
			});
		}
		else
		{
			store_result_open(store, callback);
		}
	}
	else
	{
		var options = $(".result-options", store);
		var options_height = options.height();
	
		if (options_height > 0)
		{
			store_result_close(store, callback);
		}
		else
		{
			store_result_open(store, callback);
		}
	}
}

// handler to open specified store and close the other
function store_result_focus(store, callback)
{
	// make sure we do not do useless thing when store is already active
	if (!store.hasClass("active"))
	{
		var target_store_to_close = store.parent().find(".result.active:first");
		
		if (target_store_to_close.length != 0)
		{
			store_result_close(target_store_to_close, function()
			{
				store_result_open(store, callback);
			});
		}
		else
		{
			store_result_open(store, callback);
		}
	}
	else
	{
		store_result_open(store, callback);
	}
}

// handle open specific substore in store
function store_open_option(store, type)
{
	var nid = $(".result-options .option[type='" + type + "']", store).attr("nid");
	var substore = StoreMap.substores[nid];
	
	if (substore != null)
	{
		$(".distance-value", store).text((Math.round(substore.distance_from_user * 100) / 100) + "kms");
	}
	
	// deselect all substore header in the store
	$(".result-options .option", store).each(function()
	{
		$(this).removeClass("selected");
	});
	
	// add the one that requested
	$(".result-options .option[type='" + type + "']", store).addClass("selected");
	
	// hide all substore info in the store
	$(".result-info .info", store).each(function()
	{
		$(this).addClass("hidden");
	});
	
	// show the one that requested
	$(".result-info .info[type='" + type + "']", store).removeClass("hidden");
    
    var options = $(".result-options", store);
    // readjust the height
    store_adjust_result_option(options);
}

function scroll_to_store(nid, type)
{
	var store = $("#result-container .result[name='" + nid + "']");
	store_result_focus(store, function()
	{
		store_open_option(store, type);
	});
}

var result_container = "";

function show_store_result()
{
	StoreMap.is_direct_mode = false;
	$("#result-container").show();
	$("#direct-container").hide();
	resizeWindow();
}

function show_direct_result()
{
	StoreMap.is_direct_mode = true;
	$("#result-container").hide();
	$("#direct-container").show();
	resizeWindow();
}
