// 
//  bin.js
//  Production
//  
//  Created by Todd Moore on 2010-02-08.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
// 
function form_input_classes(){

      jQuery('input[type="text"]').addClass('text');
      jQuery('input[type="password"]').addClass('text');
      jQuery('input[type="checkbox"]').addClass('checkbox');
      jQuery('input[type="radio"]').addClass('radiobutton');
      jQuery('input[type="submit"]').addClass('submit');
      jQuery('input[type="image"]').addClass('buttonImage');

}

function form_labelize(){  jQuery(".labelize input:text").clearingInput(); }   

// ========================================
// = Clear the input of the masthead form =
// ========================================
jQuery(document).ready(function() {

      form_input_classes();
      form_labelize();

});
// ===============
// = Cufon setup =
// ===============
jQuery(document).ready(function()
{
  
  Cufon.replace('.syct-btn, .call_out h2, .button, #signup input[name=submit-btn], input#submit_button, #contact_importer_button, .hd-2, .result', {fontFamily:'gotham'});
  Cufon.replace('.hd-1, .bsd h2', {fontFamily:'gotham-medium'});
  Cufon.now();
  
});

// function in place in case needed.
function load_ie_fixes()
{
  var msie_v = jQuery.browser.version;
  
  if(jQuery.browser.msie)
  {
    if(msie_v=='6.0' || msie_v == '7.0')
    {
      
    }
  }
}


// ================================================================================================
// = CHECKLIST PLUGIN, please refer to todd@bluestatedigital.com if you need to change this coode =
// ================================================================================================

// DO NOT CHANGE JAVASCRIPT BELOW THIS LINE +++++++++++++++++++++++++++++++++++++++++++++++++++++++


/**
 *  for slug arguments for the four checklist sections of the site
 */
var slugs = {s_0:null, s_1:null, s_2:null, s_3:null};

// cookie options DO NOT CHNAGE
/**
 * Opens a new date object and lets it to 365 days from now
 */
var date = new Date();
date.setTime(date.getTime() + (365 * 24 * 60 * 60 * 1000));

/**
 * Constant: sets the path to the root and expirary to one year from now
 */
var OPTIONS = { path: '/', expires: date};

/**
 * Constant: DO not alter post launch
 */
var COOKIE_NAME = "_scs-checklist_";


// Handles the changes of the get variables
/**
 * @return GET variable format "getUrlVars()['s_1']"
 */
function getUrlVars() {
	var vars = {};
	var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
		vars[key] = value;
	});
	return vars;
}

// Must be a javascript object
/**
 * @param obj JSON object in format {s_1:true, s_2:true... }
 * @return
 */
function parse_checklist(obj)
{
  for (var _obj in obj)
  {
    if(obj[_obj] === true)
    {
      _obj = _obj.split('_');
      _obj = _obj[_obj.length -1];
      _obj = parseInt(_obj);
      
      _obj = jQuery('.syct li').eq(_obj);
      _obj.addClass("completed");
      Cufon.refresh();
      
    }
  }
}

// Checks to see if checklist cookie has been set, if not, it transfers the JSON object to the cookie, if so, then resaves the required slug as the cookie
/**
 * @param _name COOKIE_NAME to return
 * @param _obj checklist values {s_1 .. }
 * @return
 */
function check_checklist_cookie(_name, _obj)
{
  // check if cookie is set, if so, parse it, if not, set a blank cookie with blank data
  if(get_cookie(_name))
  {
    parse_checklist(slugs);
    
  }else
  {
    set_cookie(_name, _obj);
  }
}

// return the cookie object and assign it to the slugs global object
/**
 * @param _name COOKIE_NAME 
 * @return object with s values from cookie 
 */
function get_cookie(_name)
{
  if(jQuery.cookie(_name))
  {
    // grab the cookie
    _temp = jQuery.cookie(_name);
    // set the slugs variable to cookies data and decode the json object BACK into a javascript object
    return slugs = jQuery.secureEvalJSON(_temp);
     
  }else
  {
    return false;
  }  
  
}

// sets the cookie info, returns false if cannot set cookie
/**
 * @param _name COOKIE_NAME
 * @param _obj save new cookie with serialized JSON object
 * @return boolean
 */
function set_cookie(_name, _obj)
{
  try
    {
      // serialize the slugs obj to JSON
      _obj = jQuery.toJSON(_obj);
      // set the cookie with JSON object and options + 1y
      jQuery.cookie(_name, _obj, OPTIONS);
      return true;
    }
  // if error completing this return false
  catch(err)
    {
      return false;
    }
}

// sets the event listeners for the check list
/**
 * Sets up the event listeners for the check list
 * 
 * @return boolean [needed to submit form] 
 * 
 */
function set_event_listeners()
{
  jQuery(document).ready(function(){
    
    //get each name in slugs
    for (var name in slugs)
    {
      //check if the get variables exist and are set 'on'
      if(getUrlVars()[name] == 'on')
      {
        // register this change to the checklist
        slugs[name] = true;
        // make the checklist visually reflect this change
        parse_checklist(slugs);
        // save the checklist slugs object to the cookie
        set_cookie(COOKIE_NAME, slugs);
      }
    }
    
    return true;
    
  });
  
  jQuery('.syct-btn.small').click(function()
  { 
    // if clicked simply set the 3 slug to true
    slugs.s_3 = true;
    if(set_cookie(COOKIE_NAME, slugs))
    {
      // parse the checklist to reflec this visually
      parse_checklist(slugs);
    }
    return true;
    
  });
  
}

// initialize and setup the checklist
jQuery(document).ready(function(){
  
  check_checklist_cookie(COOKIE_NAME, slugs);
  set_event_listeners()  
  
});


    

