/* File : sea.js */

// global modifications
if (! Array.prototype.indexOf) {
  Array.prototype.indexOf = function(obj) {
    for (var i=0; i<this.length; i++) { if(this[i]==obj){ return i; } } return -1;
  }
}
if (! Array.prototype.contains) {
  Array.prototype.contains = function(obj) {
    var index = this.indexOf(obj);
    return !(index == undefined || index == -1);
  }
}
// courtesy http://javascript.crockford.com/remedial.html
if (! String.prototype.supplant) String.prototype.supplant = function (o) {
  return this.replace(/{([^{}]*)}/g, function (a, b) {
    var r = o[b]; return typeof r === 'string' || typeof r === 'number' ? r.toString() : a;
  });
};
if (! String.prototype.safe_supplant) String.prototype.safe_supplant = function (o) {
  return this.replace(/{([^{}]*)}/g, function (a, b) {
    var r = o[b]; return typeof r === 'string' || typeof r === 'number' ? jQuery("<div/>").text(r.toString().replace(/[\r\n]+/g, 'NEWLINE')).html().replace(/NEWLINE/g, '<br />') : a;
  });
};
try {
  if (! window.console) {
    window.console = { log: function(){} };
  } else if (! window.console.log) {
    window.console.log = function() {};
  }
} catch (e) {};
jQuery.fn.truncate_text = function(rows) {
  this.each(function(index, div) {
    var message = jQuery(div).addClass('truncated');
    var text = message.text();
    message.text("W");
    rows = rows || 1;
    var row_height = message.height();
    var max_height = row_height * rows;
    message.text(text);
    if (message.height() > max_height) {
      message.text(message.text().substring(0, message.text().length * ((max_height+row_height) / message.height())));
      var loop = 0;
      while (message.height() > max_height) {
        if (loop++ > 1000) break;
        message.text(message.text().substring(0, message.text().length-1));
      }
      message.text(message.text().substring(0, message.text().length-3) + '...');
    }
  });
}

qais.require = function(jseval, fn) {
  var timeout = setTimeout(function() { qais.require(jseval, fn); }, 500);
  try {
    if (eval(jseval)) { clearTimeout(timeout); fn(); }
  } catch (e) { }
}

qais.setup_header_tools = function() {
  // construct <select/>
  function create_cost_of_things() {
    qais.cost_of_currency = {}
    var last_key = null;
    var html = [];
    html.push('<select id="cost_of_currency">');
    jQuery('#cost-of-things-table tr td').each(function(index, td) {
      if ((index % 2) == 0) {
        last_key = td.innerHTML;
        html.push('<option value="' + td.innerHTML + '">' + td.innerHTML + '</option>');
      } else if (last_key) {
        qais.cost_of_currency[last_key] = td.innerHTML;
      }
    });
    html.push('</select>');
    jQuery('.bigmacSelect').html(html.join(""));
    jQuery('#cost_of_currency').sSelect({animationSpeed:0});
  }
  create_cost_of_things();

  // radio button toggle
  jQuery("#weather_tool input[type='radio']").live('change', function(event) {
    var container = jQuery(event.target).parents('.city');
    jQuery(".display li", container).hide();
    jQuery(".display li." + event.target.id, container).show();
    jQuery.cookie("exp_weather_tool", event.target.id, { expires: 365, path: '/' });
  });
  if (jQuery.cookie("exp_weather_tool")) jQuery('#' + jQuery.cookie("exp_weather_tool")).each(function(index, ele) { ele.checked = true; }).change();
  jQuery("#localtime_tool input[type='radio']").live('change', function(event) {
    jQuery("#localtime_tool .display li").hide();
    jQuery("#localtime_tool .display li." + event.target.id).show();
    jQuery.cookie("exp_localtime_tool", event.target.id, { expires: 365, path: '/' });
  });
  if (jQuery.cookie("exp_localtime_tool")) jQuery('#' + jQuery.cookie("exp_localtime_tool")).each(function(index, ele) { ele.checked = true; }).change();
  var yahoo_url = qais.site_url + "yahoo_currency.php";

  // currency converter
  var update_currency_converter_timer = null;
  function update_currency_converter() {
    jQuery.getJSON([yahoo_url, jQuery('#from_currency').val(), jQuery('#to_currency').val()].join('/'), function(json, status) {
      jQuery('#to_currency_value').val((jQuery('#from_currency_value').val() * json['to_currency']).toFixed(3));
    });
  }
  jQuery('#from_currency,#to_currency').change(update_currency_converter);
  jQuery('#from_currency_value,#to_currency_value').keyup(function() {
    if (update_currency_converter_timer) clearTimeout(update_currency_converter_timer);
    update_currency_converter_timer = setTimeout(update_currency_converter, 800);
  });
  update_currency_converter();

  // cost of things
  function update_cost_of_things() {
    var currency1 = jQuery('#local_currency').val();
    var currency2 = jQuery('#coc_currency').val();
    if (currency1 == currency2) {
      jQuery('#coc_currency_value').val(qais.cost_of_currency[jQuery('#cost_of_currency').val()]);
    } else {
      jQuery.getJSON([yahoo_url, currency2, currency1].join('/'), function(json, status) {
        jQuery('#coc_currency_value').val(
          /* slight difference with update_currency_converter, we are getting conversion rate TO local currency */
          (qais.cost_of_currency[jQuery('#cost_of_currency').val()] / json['to_currency']).toFixed(3)
        );
      });
    }
  }
  jQuery('#cost_of_currency,#coc_currency').change(update_cost_of_things);
  update_cost_of_things();

  jQuery('#eventCountry').change(function() {
    jQuery.get(qais.site_url + '/index.php/ajaxrequests/toolsWidgetForCountry/' + jQuery('#eventCountry').val(), function(data, status) {
      var doc = jQuery('<div>' + data + '</div>');
      jQuery('#ToolsLayer').html(doc.children('#ToolsLayer').html());
      jQuery('#ToolsLayer select').sSelect({animationSpeed:0});
      qais.setup_header_tools();
    });
  });
  jQuery('#eventCity').change(function() {
    jQuery.get(qais.site_url + '/index.php/ajaxrequests/toolsWidgetForCountry/' + jQuery('#eventCountry').val() + '?city=' + jQuery('#eventCity').val(), function(data, status) {
      var doc = jQuery('<div>' + data + '</div>');
      jQuery('#ToolsLayer').html(doc.children('#ToolsLayer').html());
      jQuery('#ToolsLayer select').sSelect({animationSpeed:0});
      qais.setup_header_tools();
    });
  });
}

jQuery(document).ready(function(){
	jQuery('#eventTheme,#eventCountry,#eventCity,#select-country,#eventTime,#eventLocation,#from_currency,#cost_of_currency,#to_currency,#coc_currency').sSelect({animationSpeed:0});
	
	// Toggle Map 
	
	jQuery('.mapToggle').toggle(function(){
		jQuery(this).prev('.map').css("width", 940);
		if (window.map && map.__map) { map.__map.checkResize(); map.set_location(map.get_marker()); }
		jQuery(this).css("left",-620);
		jQuery(this).css("background-position","0 -53px");
	}, function(){
		jQuery(this).prev('.map').css("width", 320);
		if (window.map && map.__map) { map.__map.checkResize(); map.set_location(map.get_marker()); }
		jQuery(this).css("left",-34);
		jQuery(this).css("background-position","0 0");
	});
	
	// Country Tools Toggle
	
	jQuery('#CountryTools').click(function(){
    if (! qais.CountryTools) {
      qais.setup_header_tools();
      qais.CountryTools = true;
    }
		jQuery("#ToolsLayer").slideToggle();
		return false;
	});
	
	// Styling Input[type=file]
	
	/*
	$('input[type=file]').each(function(){
	    $(this).addClass('file').addClass('hidden');
	    $(this).parent().append($('<div class="fakefile" />').append($('<input type="text" />').attr('id',$(this).attr('id')+'__fake')).append($('<img src="/sea/images/buttonBrowse.jpg" alt="" />')));

	    $(this).bind('change', function() {
	      $('#'+$(this).attr('id')+'__fake').val($(this).val());;
	    });
	    $(this).bind('mouseout', function() {
	      $('#'+$(this).attr('id')+'__fake').val($(this).val());;
	    });
	  });
	*/
	
	// Increase Clickable Area - Requested by shah 19 Dec 2009
	
	jQuery('.subLevel .listing dl dt a').bigTarget({
		clickZone: 'dl:eq(0)'
	});
	
	// Facebox 
	
	//jQuery(document).ready(function($) {
	  //$('a[rel*=facebox]').facebox()
	//})
	
	// Print Page 
	
	jQuery('.printPage').click(function(){
		window.print();
		return false;
	});
	
});

//print function 

//fix for footer overlapping in ie8 browser
function fixIE8Footer(){
  $("#ie8footerfix").show(0);
  $("#ie8footerfix").hide(0);
}

