﻿(function()
{
    /**
     *  Fix IE background image flicker
     *  http://www.mister-pixel.com/
     */
    try
    {
        document.execCommand('BackgroundImageCache', false, true);
    }
    catch(e)
    {}
})();

FishfulThinking = {};

function FireActionTag(tag){
 var trackingImage = document.getElementById('action_tag');
 trackingImage.src = 'http://view.atdmt.com/action/' + tag;
} 

sfHover = function() {
	var sfEls = document.getElementById("navigation").getElementsByTagName("LI");
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function() {
			this.className+=" sfhover";
		}
		sfEls[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
		}
	}
	var sfEls2 = document.getElementById("search").getElementsByTagName("LI");
	for (var i=0; i<sfEls2.length; i++) {
		sfEls2[i].onmouseover=function() {
			this.className+=" sfhover";
		}
		sfEls2[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
		}
	}
}
if (window.attachEvent) window.attachEvent("onload", sfHover);

function navigateComponent(controlName)
{
    // if user clicks before page is loaded then ignore the click
    if (typeof(widgetNavData) =="undefined") return;
    
    // otherwise navigate the widget.
    if (widgetNavData)
    {
        for(var i = 0; i < widgetNavData.length; i++)
        {
           if (widgetNavData[i].ControlName == controlName)
           { 
                var restUrl = "/" + widgetNavData[i].RestId;
                window.location.href = restUrl;
           }
        }
    }
}

function initQuickFinder()
{
    $('#situation_list li a').bind("click", function(){
        $('#situation_menu').val(this.rel);
        navigateQuickFinder();
    });

    var finder = $("#quick-finder");
    $("#quick-finder-link").bind("click", {finder: finder}, showQuickFinder); 
    $("#quick-finder #close").bind("click", {finder: finder}, closeQuickFinder); 
}

function showQuickFinder(e)
{
    var finder = e.data.finder;
    $(finder).css({left: 'auto'});
    $(finder).css({right: '30px'}).addClass('active');
    return false;
}
function closeQuickFinder(e)
{
    var finder = e.data.finder;
    $(finder).css({left: '-9999em'});
    $(finder).css({right: 'auto'}).removeClass('active');
    return false;
}

function navigateQuickFinder()
{
    var age_menu = $('#age_menu').val();
    if(age_menu=="") { age_menu = "5"; }
    
    var skill_menu = $('#skill_menu').val();
    if(skill_menu=="") { skill_menu = "AllSkills"; }
    
    var situation_menu = $('#situation_menu').val();
    if(situation_menu=="") { situation_menu = "None"; }
    
    var searchUrl = "/Search/ContentFinder/" + age_menu + "/" + skill_menu + "/" + situation_menu;
    document.location = searchUrl;
}
$(document).ready(initQuickFinder);


/* BEGIN - Ask Dr. Reivich character count functions */
function enableCharacterCount()
{
	var x = document.getElementsByTagName('textarea');
	var counter = document.getElementById('maxCharacters');
	for (var i=0;i<x.length;i++)
	{
		if (x[i].getAttribute('rel'))
		{
			x[i].relatedElement = counter;
			x[i].onkeyup = x[i].onchange = checkMaxLength;
			x[i].onkeyup();
		}
	}
}

function checkMaxLength()
{
	var maxLength = parseInt(this.getAttribute('rel'));
	var currentLength = this.value.length;
	var remainingLength = maxLength - currentLength;
	this.value = this.value.substring(0,maxLength);
	if (currentLength > maxLength)
	{
		this.value = this.value.substring(0,maxLength);
		this.relatedElement.value = 0;
	}
	else
	{
		this.relatedElement.value = remainingLength;
	}
}

$(document).ready(enableCharacterCount);
/* END - Ask Dr. Reivich character count functions */

/*hopefully can refactor with lightbox soon*/
function popup(url, windowName, width, height, params) {
	params ? params = ', '+params : params='';
	window.open(url,windowName,'width='+width+',height='+height+params);
}

function URLEncode (clearString) {
  var output = '';
  var x = 0;
  clearString = clearString.toString();
  var regex = /(^[a-zA-Z0-9_.]*)/;
  while (x < clearString.length) {
    var match = regex.exec(clearString.substr(x));
    if (match != null && match.length > 1 && match[1] != '') {
    	output += match[1];
      x += match[1].length;
    } else {
      if (clearString[x] == ' ')
        output += '+';
      else {
        var charCode = clearString.charCodeAt(x);
        var hexVal = charCode.toString(16);
        output += '%' + ( hexVal.length < 2 ? '0' : '' ) + hexVal.toUpperCase();
      }
      x++;
    }
  }
  return output;
}

function customOmniLink(obj, type){
	var s_click = s_gi(s_account);
	s_click.events='event14';
	s_click.linkTrackVars = 'events,eVar9';
	s_click.linkTrackEvents = 'event14';
	s_click.eVar9 = type;
	s_click.tl(this, 'o', type);
}

function productImageSwap() {
	$("div#productdetailthumbs a").click(function() {
		var chosenImage = $(this).attr("rel");
		$("div.productdetailthumb").removeClass("selectedthumb");
		$(this).parent("div.productdetailthumb").addClass("selectedthumb");
		$("img#productdetailimage").attr("src",chosenImage);
		return false;
	});
}
$(document).ready(productImageSwap);

FishfulThinking.Navigation = function(config) {
    this.container = config.container;
    
    this.init();
};
FishfulThinking.Navigation.prototype = {
    init: function()
    {
        // DOM elements
        this.$listItemsWithSubNav = this.container.find('li:has(ul)');
        this.$subNavLists = this.container.find('li > ul');
        // setup methods
        this.setup();
        this.attachEvents();
    },
    
    setup: function()
    {
        // for bottom graphic
        this.$subNavLists.append('<li class="end"></li>');
    },
    
    attachEvents: function()
    {
        this.$listItemsWithSubNav.bind('mouseenter', this.toggle).bind('mouseleave', this.toggle);
    },
    
    toggle: function(event)
    {
        var link = $('a:first', this);
        var list = $(this).find('ul');
        if (event.type == 'mouseenter')
        {
            link.addClass('active');
            list.show();
        }
        else
        {
            link.removeClass('active');
            list.hide();
        }
    }
};

FishfulThinking.Modal = function(config) {
    this.container = config.container;
    
    this.init();
};
FishfulThinking.Modal.prototype = {
    init: function()
    {
        // cache DOM elements
        this.head = $('head')
        // bound/prox'd method calls
        this.bShow = $.proxy(this.show, this);
        this.bHide = $.proxy(this.hide, this);
        // set up methods
        this.setup();
        this.attachEvents();
    },
    
    setup: function()
    {
        // create print CSS reference
        this.printCssFile = $('<link type="text/css" rel="stylesheet" media="print" href="/styles/print-modal.css" />');
        // init jqModal
        this.container.jqm({overlay: 68, toTop: true, onHide: this.bHide});
    },
    
    attachEvents: function()
    {
        $(document).bind('RecipeModal:open', this.bShow);
    },
    
    show: function()
    {
        // insert print CSS
        this.head.append(this.printCssFile);
        // show modal
        this.container.jqmShow();
    },
    
    hide: function(hash)
    {
        // remove print CSS
        this.printCssFile.remove();
        // remove modal
        hash.w.hide();
        hash.o.remove();
    }
};

function openRecipeModal()
{
    $(document).trigger('RecipeModal:open');
}
