/*
 * All Contents Copyright 2009 Olio. All Rights Reserved.
 *
 * http://www.oliodesign.co.uk
 *
 * You cannot use or copy this script without first asking persmission!
 *
*/

$(document).ready(function() {


  //Custom radio auto submit
  $('.customRadioAutosubmit').each(function() {
  
	   $(this).click(function(event) {
	   
	     $(this).parents('form').submit();
	     
	   });
  });
  
  

  $('.customSelect').each(function() {

    var options=$(this).find('option');
    var replacement='';
    var replacement_options=new Array();
    var selected = '';
    var inputName=$(this).attr('name');
    
    if(inputName=='') { alert('Warning: this has no input name so custom select will break.'); }
        
    // Load all the options into an array
    var i=1;
		var style = "";
    $.each(options, function() {
      replacement_options.push('<div class="customSelectReplacementOption  ' + $(this).attr('class') + '"><span style="display:none;" class="index">' + i + '</span><span class="value" style="display:none;">' + $(this).attr('value') + '</span><span class="label">' + $(this).text() + '</span></div>');
      
      if($(this).is(':selected')) {
        selected='<div class="customSelectReplacementOption"><span class="value" style="display:none;">' + $(this).attr('value') + '</span><span class="label">' + $(this).text() + '</span></div>';
      }

      i++;
    });
    
    
    // Build the replacement structure
    replacement='<div class="customSelectReplacement" id="customSelect_' + $(this).attr('name') + '">';
    
    if(selected=='') {
      replacement+=replacement_options[0];
    }else{
      replacement+=selected;
    }
    
    replacement+='<div class="customSelectReplacementDropdown">' + replacement_options.join('') + '</div><input class="customSelectReplacementInputValue" type="hidden" name="' + $(this).attr('name') + '" value="" /></div>';
    
    
    // Place into the document and format
    $(this).after(replacement).hide();
    
    var newReplacement=$('.customSelectReplacement#customSelect_' + $(this).attr('name'));
    newReplacement.css('width',$(this).css('width'));
    var newReplacement_dropdown=newReplacement.find('.customSelectReplacementDropdown');
    var newReplacement_options=newReplacement_dropdown.find('.customSelectReplacementOption');
    newReplacement_dropdown.css('width',$(this).css('width'));
    newReplacement.find('.customSelectReplacementOption:first').addClass('customSelectReplacementSelected');
    var newReplacement_toprow=newReplacement.find('.customSelectReplacementSelected');
    var newReplacement_value=newReplacement.find('.customSelectReplacementInputValue');
    newReplacement_value.val(newReplacement_toprow.find('.value').html());
    
    
    // Open and close the dropdown
    newReplacement_toprow.click(function(event) {
      event.stopPropagation();
      if(newReplacement_dropdown.is(':hidden')) {
        $('.customSelectReplacementDropdown').not(newReplacement_dropdown).slideUp(150);
        newReplacement_dropdown.slideDown(150);
      }else{
        newReplacement_dropdown.slideUp(150);
      }
    });
    
    // Select an item
    newReplacement_options.each(function() {
      $(this).click(function(event) {
        event.stopPropagation();
        newReplacement_toprow.find('.value').html($(this).find('.value').html());
        newReplacement_toprow.find('.label').html($(this).find('.label').html());
        newReplacement_value.val($(this).find('.value').html());
        newReplacement_dropdown.slideUp(150);
        
        if( $('select[name="' + inputName + '"]').is('.jumpbox') ) {
          window.location=html_entity_decode($(this).find('.value').html());
        }
        
        if( $('select[name="' + inputName + '"]').hasClass('customSelectAutosubmit') ) {
          $('select[name="' + inputName + '"]').parents('form').submit();
        }
        
      });
    });
    
    
  });

  // Hide the menus if user clicks outside of element
  $('html').click(function() {
   $('div.customSelectReplacementDropdown').slideUp(150);
  });
  
});

function html_entity_decode(str)
{
    try
	{
		var  tarea=document.createElement('textarea');
		tarea.innerHTML = str; return tarea.value;
		tarea.parentNode.removeChild(tarea);
	}
	catch(e)
	{
		//for IE add <div id="htmlconverter" style="display:none;"></div> to the page
		document.getElementById("htmlconverter").innerHTML = '<textarea id="innerConverter">' + str + '</textarea>';
		var content = document.getElementById("innerConverter").value;
		document.getElementById("htmlconverter").innerHTML = "";
		return content;
	}
}
