/**
 * (c) Copyright 2008 Oakley, Inc.
 *
 */
// Global
var dealerMarkup = "";
var items_per_page = 2;

// Lets start this!
$(document).ready(function(){
	// Build our tabs for the bottom | Features / Tech Specs / Gallery
	$("#product_information_container").tabs();
	// Drop Down Menu
    // Variables
    var dropdown = "";
    var dropdownTimeout = 500;
    var dropdownTimer = 0;
    var dropdownMenu = 0;
       
    // Our Action Listeners
    $("li.dropdown_menu_main_item").bind('mouseover', dropdown_open);
    $("li.dropdown_menu_main_item").bind('mouseout',  dropdown_timer);
    
    // Open
    function dropdown_open() {
      dropdown_cancel_timer();
      dropdown_close();
      dropdownMenu = $(this).find('ul').css('display', 'block');
      $(this).addClass("selected");
    }
    
    // Close
    function dropdown_close(dropdown) {
      if(dropdownMenu) dropdownMenu.css('display', 'none');  
      $("li.dropdown_menu_main_item").removeClass("selected");
    }
    
    // Timer
    function dropdown_timer() {
      dropdownTimer = window.setTimeout(dropdown_close, dropdownTimeout);
    }
    
    // Cancel Timeout
    function dropdown_cancel_timer() {
      if(dropdownTimer){
        window.clearTimeout(dropdownTimer);
        dropdownTimer = null;
      }
    }
});

// Build a mini list based on multiple products passed as an array
function build_dealer_list(productArray,container,items_per_page){
	// Lets get our JSON of Dealers
	$.getJSON("/js/pages/elite/dealer_list.js", function(jsonData){
		// Callback function
		build_dealer_list_markup(jsonData,productArray,container,items_per_page);
	});
}

// Build a full listing of dealers based on a single product
function build_dealer_list_markup(dealerJSON,productArray,container,items_per_page){
	intCounter = 0;
	var intItemCounter = 1;
	// Make sure that our JSON data is there
	if(dealerJSON != null){
		dealerMarkup += '<div class="dealers_list">';
		// Loop through and grab the dealers based on what
		$.each(dealerJSON.dealers, function(i, dealer_item) {
			var enable_dealer = false;
			// Loop through what products we are looking for
			$.each(dealer_item.products, function(ii, productItem) {				
				// Loop through what products we are looking for
				$.each(productArray, function(iii, productItemCheck) {
					// Check to see if we have enabled this deal yet for this list.
					if(enable_dealer == false){
						// Check to make sure that what we check against exists
						if(productItem[productItemCheck] != null){
							//See if we add this dealer to the list
							if(productItem[productItemCheck] == 'true'){
								//First item so we need a opening tag
								if(intItemCounter == 1){
									// Start building our page markup for the dealer list
									dealerMarkup += '<div class="dealers_list_item_container">';
								}
								//
								add_dealer_to_markup(dealer_item,intCounter);
								// See if this is the last one then add our closing tag
								if(intItemCounter == items_per_page){
									dealerMarkup += '</div>';
									intItemCounter = 1;
								}else{
									intItemCounter++;
								}
								// Add to counter
								intCounter++;
								// Set to true so we dont disable or re-enable this dealer for the listing
								enable_dealer = true;
							}	
						}
					}					
				});
			});
		});
		
		// Cleanup - make sure that if the last group did not recieve a closing tag that we add one
		if(intItemCounter < items_per_page && intItemCounter > 1){
			dealerMarkup += '</div>';
		}
			
		// Done building the markup for the dealer list
		dealerMarkup += '</div>';
		// Lets dump our dealer list markup into the page
		$(container + ' .dealer_list_placeholder').html(dealerMarkup);
		
		// Dealer Listing Scroller
		var api = $(container + ' .dealer_list_placeholder').scrollable({
			size: 1,
			vertical: true
		}).navigator({api: true});

		// Set the total number of pages
		$(container + ' .totalPages').html(api.getPageAmount());
		
		// Paging through dealers
		api.onSeek(function() {
			// Set the page number
 			$(container + ' .currentPage').html(api.getPageIndex()+1);
		});	
	}else{
		// Error Handling
		// Put text inside dealer box stating dealer list error
		$(container + ' .dealer_list_placeholder').html('<p>There was an error loading dealers.<p>');
	}
	// Resource Cleanup
	dealerMarkup = "";
}

// Add a dealer to the markup
function add_dealer_to_markup(dealer_item,i){
	dealerMarkup += '<div class="dealer">' +
						'<div class="number">' + (i + 1) + '</div>' +
						'<div class="vcard">' +
							'<div class="adr">' +
								'<div class="fn org">' + dealer_item.location + '</div>' +
								'<div class="street-address">' + dealer_item.address + '</div>' +
								'<div class="extended-address"></div>' +
								'<span class="locality">' + dealer_item.city + ' </span>' +
								'<span class="region">' + dealer_item.state + ' </span>' +
								'<span class="postal-code">' + dealer_item.zip + '</span>' +
								'<div class="country-name">' + dealer_item.country + '</div>' +
							'</div>' +
							'<div class="tel">' + dealer_item.phone + '</div>' +
						'</div>' +
					'</div>';
}
