// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults
/*
	Object.prototype.methods = function(ownMethodsOnly){
		// makes debugging a little easier...
		var methods = [];
		for (prop in this){
		  if (ownMethodsOnly){
		    if ( this.hasOwnProperty(prop) ){methods.push(prop);}
		  }
		  else{
		    methods.push(prop);
		  }
		}
		return methods.sort().join(" , ");
	}

*/	
	
var trackingLinks = function(elemId){
	//add the tracking code to links with class "track"
	// where the tracking id is in a class name like:
	// id class="track code-global_nav+log_in_go"
	var elements = "";
	if (elemId){
		elements = $("#" + elemId + " .track");
	}
	else{
		elements = $(".track");
	}
	elements.each(function() {
		var trackingCodes = this.getAttribute("data-code");
		trackingCodes = trackingCodes ? trackingCodes.split('+') : [];
		for (var i = 0; i < trackingCodes.length; i++){
			trackingCodes[i] = trackingCodes[i].toUpperCase();
		}
		//console.log("GA tracking code added: " + trackingCodes);

		var callbackFunction = function(){
			var res = pageTracker._trackEvent.apply(this, trackingCodes);
			//console.log(trackingCodes.join("/"), "tracking executed");
			return true;
		}
		if ($(this).attr("class").match(/mouseover/i)){
			$(this).mouseover(callbackFunction);
		} else if ($(this).attr("class").match(/change/i)){ 
			$(this).change(callbackFunction);
		}
		else{
			$(this).click(callbackFunction);
		}
	});
};

$(document).ready(function(){
    //$('body').supersleight({shim: '/images/blank.gif'});
	trackingLinks();

    $('.hoverableShow').hover(
      function(){},
      function(){
      $('.ratedImg').css('z-index', '0');
      $(this).parent('.hoverableImage').find('.hoverableShow').css('display', 'none');
      });

    $('.hoverable').mouseover(
      function(){
      $('.hoverableShow').css('display', 'none');
      $('.ratedImg').css('z-index', '0');

      $(this).css({'z-index': '100', 'position': 'relative'});
      $(this).parent('.hoverableImage').find('.hoverableShow').show();
      });

    
    $('.judgeImg').mouseover(
        function(){
        jId = "#bio_" + this.id;
        $('.judgeCopy').hide();
        $(jId).show("fast");
        });


    $('.buttonLink').hover(
      function(){
      element = $(this).find('img');
      element.attr('src', element.attr('src').replace(/\.gif/, "_black.gif"));
      },
      function(){
      element.attr('src', element.attr('src').replace(/_black.gif/, ".gif"));
      });


    $('.inspiredHoverableShow').hover(
        function(){},
        function(){
        $('.callout_product').css('z-index', '0');
        $(this).parent('.callout_product').find('.inspiredHoverableShow').css('display', 'none');
    });

    $('.inspiredHoverable').mouseover( function() {
        $('.inspiredHoverableShow').css('display', 'none');
        $(this).parent('.callout_product').find('.inspiredHoverableShow').show();
    });

    /*
       $('.hoverable').hover(
       function(){
       $(this).css({'z-index': '100', 'position': 'relative', 'height': '100%'});
       $(this).parent('.hoverableImage').find('.hoverableShow').show();
       $(this).parent('.hoverableImage').find('img').css('padding-bottom', '70px');
       $(this).parent('.hoverableImage').find('.ratings').css({'float': 'left', 'z-index': '120'});

       },
       function(){
       $('.ratedImg').css('z-index', '0');
       $(this).parent('.hoverableImage').find('.hoverableShow').css('display', 'none');
       });
       */



    /* 

    // Version to allow show 'hoverableShow' on mouseover 'hoverable', but hide it when mouseout 'hoverableShow'
    //
    $('span.hoverable').mouseover(function(){
    $(this).css({'z-index': '100', 'position': 'relative'});
    $(this).parent('.hoverableImage').find('.hoverableShow').show();
    })
    $('span.hoverableShow').mouseout(function(){
    $('.hoverable').css('z-index', '0');
    $(this).parent('.hoverableImage').find('.hoverableShow').css('display', 'none');    
    });
    */ 

  $('div#chosenSliderGallery').each(function () {
    var ul = $('ul', this);
    var items = $('li', ul);
    var num_chosens = Math.max(3, items.length) - 2;
    
    // Note: 230 is the width of a chosen pic.  
    var chosenWidth = num_chosens * 230;
  
    var slider = $('#chosenSlider', this).slider({ 
      handle: '#chosenHandle',
      minValue: 0, 
      maxValue: chosenWidth, 
      slide: function (ev, ui) {
        ul.css('left', '-' + ui.value + 'px');
      }, 
      stop: function (ev, ui) {
        ul.animate({ 'left' : '-' + ui.value + 'px' }, 500, 'linear');
      }
    });
  });

});


//switches #user_password from input type="text" to type="password"
//so it can show regular text on page load, then switch to password field/text 
//obfuscation onFocus
//
$(document).ready(function()
    {
    $("#user_password").focus(function(srcc)
      {
      document.getElementById('user_password').type="password";
      })
    });
//end user_password script

//jQuery script to include default text for Log In on Home Page, clear onFocus
//
$(document).ready(function()
    {
    $(".login_field").focus(function(srcc)
      {
      if ($(this).val() == $(this)[0].title)
      {
      $(this).removeClass("defaultTextActive");
      $(this).val("");
      }
      });

    $(".login_field").blur(function()
      {
      if ($(this).val() == "")
      {
      $(this).addClass("defaultTextActive");
      $(this).val($(this)[0].title);
      }
      });

    $(".login_field").blur();        
    });
//end default text script


// styling for select menus
(function ($) {

 $.fn.select_skin = function (w) {
 return $(this).each(function(i) {
   var s = $(this);

   // create the container
   s.wrap('<div class="cmf-skinned-select"></div>');
   var c = s.parent();
   c.children().before('<div class="cmf-skinned-text">&nbsp;</div>').each(function() {
     if (this.selectedIndex >= 0) $(this).prev().text(this.options[this.selectedIndex].innerHTML)
     });
   c.width(s.outerWidth()-2);
   c.height(s.outerHeight()-2);

   // skin the container
   c.css('background-color', s.css('background-color'));
   c.css('position', 'relative');

   // hide the original select
   s.css( { 'opacity': 0,  'position': 'relative', 'z-index': 100 } );

   // get and skin the text label
   var t = c.children().prev();
   t.height(c.outerHeight()-s.css('padding-top').replace(/px,*\)*/g,"")-s.css('padding-bottom').replace(/px,*\)*/g,"")-t.css('padding-top').replace(/px,*\)*/g,"")-t.css('padding-bottom').replace(/px,*\)*/g,"")-2);
   t.width(c.innerWidth()-s.css('padding-right').replace(/px,*\)*/g,"")-s.css('padding-left').replace(/px,*\)*/g,"")-t.css('padding-right').replace(/px,*\)*/g,"")-t.css('padding-left').replace(/px,*\)*/g,"")-c.innerHeight());
   //t.css('color', s.css('color'));
   t.css('color', "white");
   t.css('font-size', s.css('font-size'));
   t.css('font-family', s.css('font-family'));
   t.css('font-style', s.css('font-style'));
   t.css( { 'opacity': 100, 'overflow': 'hidden', 'position': 'absolute', 'text-indent': '0px', 'z-index': 1, 'top': 0, 'left': 0 } );

   // add events
   c.children().click(function() {
       t.text(this.options[this.selectedIndex].innerHTML);
       });
   c.children().change(function() {
       t.text(this.options[this.selectedIndex].innerHTML);
       });
 });
 }
}(jQuery));
jQuery(document).ready(function() {
    $("#results_order").select_skin();
    });
jQuery(document).ready(function() {
    $("#gender_id").select_skin();
    });
jQuery(document).ready(function() {
    $("#hair_length_id").select_skin();
    });
jQuery(document).ready(function() {
    $("#hair_color_id").select_skin();
    });
jQuery(document).ready(function() {
    $("#image_creative_gender_id").select_skin();
    });
jQuery(document).ready(function() {
    $("#image_creative_hair_length_id").select_skin();
    });
jQuery(document).ready(function() {
    $("#image_creative_hair_color_id").select_skin();
    });
jQuery(document).ready(function() {
    $("#contest_id").select_skin();
    });



//Javascript to handle showing the register nudge the first time a non-logged-in user votes.
var hasRated = false;
function showRegisterNudge(url){
  if(!hasRated) {
    hasRated=true;
    tb_show("", url+"?height=220&width=400&modal=true");
  }
  return false;
}


/* INPUT TYPE = "FILE" STYLING */
(function($) {    
 $.fn.filestyle = function(options) {              
 /* TODO: This should not override CSS. */
 var settings = {
width : 250
};

if(options) {
$.extend(settings, options);
};

return this.each(function() {         
  var self = this;
  var wrapper = $("<div class='browseButton'>")
  .css({
    "width": settings.imagewidth + "px",
    "height": settings.imageheight + "px",
    "background": "url(" + settings.image + ") 0 0 no-repeat",
    "background-position": "top left",
    "display": "inline",
    "position": "absolute",
    "overflow": "hidden",
    "cursor": "pointer",
    "margin-top": "3px"
    });

  var filename = $('<input class="file">')
  .addClass($(self).attr("class"))
  .css({
    "display": "inline",
    "width": settings.width + "px"
    });

  $(self).before(filename);
  $(self).wrap(wrapper);

  $(self).css({
      "position": "relative",
      "height": settings.imageheight + "px",
      "width": settings.width + "px",
      "display": "inline",
      "cursor": "pointer",
      "opacity": "0.0"
      });

  if ($.browser.mozilla) {
    if (/Win/.test(navigator.platform)) {
      $(self).css("margin-left", "-142px");                    
    } else {
      $(self).css("margin-left", "-168px");                    
    };
  } else {
    $(self).css("margin-left", settings.imagewidth - settings.width + "px");                
  };

  $(self).bind("change", function() {
      filename.val('file://.../' + $(self).val()); //make them think its real
      $(self).parent('div').parent('li').find('input').css({
        "background-color": "#333333", 'color':'white'}); 
      });
});       
};    
})(jQuery);

/* individual input type="file" elements styled */
$(function() {
    $("input.fileField").filestyle({ 
        image: "/images/btn_browse.jpg",
        imageheight : 25,
        imagewidth : 69,
        width : 250
        });
    });

