var IE6 = false /*@cc_on || @_jscript_version < 5.7 @*/;
var current_rollover = null;
var rollover_timer = null;
var rollover_sleep_timer = null;

document.observe("dom:loaded", init_dom);

Event.observe(window, 'load', init, false);

function init(evt) {
	if($('map')){
		loadMap();
	}

	//resize all coupon body tags
	biggest_div = null;
	$$('#coupon_list div.coupon div.body').each(function(obj) {
		if(biggest_div==null || biggest_div.getHeight() < obj.getHeight()){
			biggest_div = obj
		}
	});

	if(biggest_div != null){
	  
	  var regex = new RegExp("Windows NT 6.0;");
	  var vista = (regex.test(navigator.appVersion));
	  var objects = $$('#coupon_list div.coupon div.body');
	  
	  if(Prototype.Browser.IE && !vista){
	    new_height = biggest_div.getHeight();
	    objects = $$('#coupon_list div.coupon');
	  }else{
	    new_height = biggest_div.getHeight() - (biggest_div.getStyle('padding-bottom').match(/[0-9]+/)[0]);
	  }
		objects.each(function(obj) {
			obj.setStyle({height: new_height+'px'});
		});
	}
}


function init_dom(evt) {


  if ($('sort-by')) {
    Event.observe('sort-by', 'click', toggleOptions, false);
  }
  if ($('view-options')) {
    Event.observe('view-options', 'click', toggleOptions, false);
  }

// ERRORS IN IE
  // Event.observe('content', 'click', openInNewWindow, false);
  // Event.observe('content', 'click', printWindow, false);
  // Event.observe('content', 'click', toggleAnswer, false);
  // hideAnswers();

  Event.observe(document.getElementsByTagName('HTML')[0], 'click', hideDropDowns, false);
  preloadDefaultSearchTerms();

  // Coupon rollover
  if ($('coupon_list')) {
    // Event.observe($('coupon_list'), 'mouseover', couponRollover, false);
    $$('#coupon_list div.coupon img').each(function(item) {
      Event.observe(item, 'mouseover', couponRollover, false);
      Event.observe(item, 'mouseout', clearAllRollovers, false);
    });
  }
}

function toggleOptions(evt) {
  var elt = Event.element(evt);
  while (elt.className != 'selected' && elt.tagName != 'A') {
    if (elt.className == 'drop-down-selected') {
      ul = $$('div#' + elt.parentNode.id + ' ul')[0];
      Effect.toggle(ul, 'blind', {duration: 0.25});
      break;
    } else {
      elt = elt.parentNode;
    }
  }
}

function openInNewWindow(evt) {
  var elt = Event.element(evt);
  while (elt.id != 'content') {
    if (elt.hasClassName('new-window')) {
      window.open(elt.href);
      Event.stop(evt);
      break;
    } else {
      elt = elt.parentNode;
    }
  }
}

function printWindow(evt) {
  var elt = Event.element(evt);
  while (elt.id != 'content') {
    if (elt.hasClassName('print')) {
      window.print();
      Event.stop(evt);
      break;
    } else {
      elt = elt.parentNode;
    }
  }
}

function hideDropDowns(evt) {
  var elt = Event.element(evt);
  var hideElements = ['category-breadcrumb-list', 'more-nav', 'sort-by-list', 'view-options-list'];
  while (elt.tagName.toUpperCase() != 'HTML') {
    if (elt.tagName.toUpperCase() != 'BODY') {
      hideElements.each(function(item) {
        if ($(item) && $(item).getStyle('display') == 'block') {
          Effect.toggle(item, 'blind', {duration: 0.25});
        }
      });
      break;
    } else {
      elt = elt.parentNode;
    }
  }
}

function hideAnswers() {
  var answers = $('content').getElementsByClassName('answer');
  answers.each(function(answer) {
    answer.setStyle({display: 'none'});
  });
}

function toggleAnswer(evt) {
  var elt = Event.element(evt);
  while (elt.id != 'content') {
    if (elt.hasClassName('question')) {
      answers = elt.parentNode.getElementsByClassName('answer');
      answers.each(function(answer) {
        Effect.toggle(answer, 'appear', {duration: 0.25});
      });
      break;
    } else {
      elt = elt.parentNode;
    }
  }
}

function item_clicked(evt){
 var child = Event.element(evt);
 alert('The child node with id=' + child.id + ' was clicked');
 Event.stop(evt); //avoid another call related to 'parent_node' itself
}


function preloadDefaultSearchTerms() {
  if ($('search_zavers')) {
    if ($F('terms').length > 0) {
      $('search_zavers').addClassName('access');
    }
  }
}

function hideDefaultSearchTerms() {
  $('search_zavers').addClassName('access');
}

function showDefaultSearchTerms(value) {
  if (value.length == 0) {
    $('search_zavers').removeClassName('access');
  }
}

function couponMoreLinks(evt) {
  var elt = Event.element(evt);
  while (elt.id != 'content') {
    if (elt.hasClassName('more') || elt.hasClassName('more_information')) {
      learn_more_widget.show();
      new Ajax.Request(elt.href, {asynchronous:true, evalScripts:true});
      break;
    } else {
      elt = elt.parentNode;
    }
  }
}


function termsconditions() {
  learn_more_widget.show();
  var response = new YAHOO.widget.Panel("response_widget", 
  { 
    width: '400px',
    close:true,
    visible:false,  
    draggable:false,
    modal:true,
    fixedcenter: true,
    effect:{effect:YAHOO.widget.ContainerEffect.FADE,duration:0.25},
    zindex:100
  }); 
  response.setHeader('Terms & Conditions');
  response.setBody('If you provide your mobile number and click &quot;Register,&quot; Zavers will send you up to 10 alerts a month describing new offers and updates. In addition, when you see the Zavers short code on a coupon, you can text 224466 and reference the 6-digit code on the coupon to receive that offer and add it to your Zavers account via your phone. There is not a charge for either of these services from Zavers, but Msg &amp; Data Rates May Apply.<br /><br />You can unsubscribe from Zavers Alerts at any time by texting &quot;STOP&quot; to 224466.<br /><br />If you have questions text &quot;HELP&quot; to 224466 or contact us at webcontact@zavers.com');
  response.setFooter('Copyright © 2008 Zavers');
  response.render(document.body);
  response.beforeHideEvent.subscribe(function(){ $('learn_more_image').innerHTML = ''; });

  hide_old_YUI_panels();
  response.show();
}

function termsAndConditionsLinks(evt) {
  var elt = Event.element(evt);
  while (elt.id != 'content') {
    if (elt.hasClassName('more')) {
      learn_more_widget.show();
      new Ajax.Request(elt.href, {asynchronous:true, evalScripts:true});
      break;
    } else {
      elt = elt.parentNode;
    }
  }
}

function hide_old_YUI_panels() {
  $$(".yui-panel-container").each(function(x){ x.setStyle({ visibility: 'hidden' }) });
  $$(".mask").each(function(x){ x.hide() });
}

function clearAllRollovers() {
  $$(".rollover").each(function(x) { x.setStyle({ left: "-1000em"}); });
  current_rollover = null;
  clearTimeout(rollover_timer);
}

function couponRollover(evt) {
  var elt = Event.element(evt);
  var id = elt.up().id.split('-')[1];
  current_rollover = 'rollover-' + id;
  var rollover = $(current_rollover);
  var position = Element.positionedOffset($('coupon-' + id));
  var left_pos = -218 - elt.width; //callout right

  rollover_timer = setTimeout(function () {
    clearAllRollovers();
    if ((position[0] > 0 && position[0] < 50) || position[0] < -300){
      left_pos = 208; //callout left
      rollover.removeClassName("roll_right");
      rollover.addClassName("roll_left");
    }
    rollover.setStyle({ left: position[0] + left_pos + "px", top: position[1] + "px" });
  }, 800);
  
}

function open_player(url, campaign_id){
	new Ajax.Request(url, {parameters: { campaign_id: campaign_id}, asynchronous:true, evalScripts:true});
}

function close_player(campaign_id){
	$('media_player_'+ campaign_id).innerHTML = '';
}
