/* jQuery elementReady plugin
 * Version 0.6
 * Copyright (C) 2007-08 Bennett McElwee.
 * Licensed under a Creative Commons Attribution-Share Alike 3.0 Unported License (http://creativecommons.org/licenses/by-sa/3.0/)
 */
(function($) {



var interval = null;
var checklist = [];
$.elementReady = function(id, fn) {
	checklist.push({id: id, fn: fn});
	if (!interval) {
		interval = setInterval(check, $.elementReady.interval_ms);
	}
	return this;
};

// Plugin settings
$.elementReady.interval_ms = 23; // polling interval in ms

// Private function
function check() {
	var docReady = $.isReady; // check doc ready first; thus ensure that check is made at least once _after_ doc is ready
	for (var i = checklist.length - 1; 0 <= i; --i) {
		var el = document.getElementById(checklist[i].id);
		if ( (el && (el.nextSibling || el.parentNode.nextSibling))|| docReady) {

			var fn = checklist[i].fn; // first remove from checklist, then call function
			checklist[i] = checklist[checklist.length - 1];
			checklist.pop();
			fn.apply(el, [$]);
		}
	}
	if (docReady) {
		clearInterval(interval);
		interval = null;
	}
}

})(jQuery);

(function($) {
  $.fn.timePopUp = function(options){
  	var $this = $(this);
  	var initFn = null;
  	var settings = $.extend({
  	trigger : $this,
  	url	: $this.attr('href'),
  	name : '',
  	height : 600,
  	width : 600,
  	popupOptions : 'toolbar=0,scrollbars=1,location=1,statusbar=1,menubar=0,resizable=1'
  	}, options);
  	if(settings.initFn){settings.initFn();}
  	settings.popupOptions +=',width='+settings.width+',height='+settings.height+',left = '+(screen.width - settings.width) / 2+',top ='+(screen.height - settings.height) / 2;
  	settings.trigger.click(function(event){
  	    event.preventDefault();
  	    
  			var newwindow = window.open(settings.url,settings.name,settings.popupOptions);

  			if (window.focus) {
          newwindow.focus();
        }
  			 event.stopPropagation();
  	});
  };

})(jQuery);

function isValidEmail(str) {
	var filter  = /^([a-zA-Z0-9_.-/+])+@(([a-zA-Z0-9-])+\.)+([a-zA-Z0-9]{2,6})$/;
	return (filter.test(str));
}

// Quigo 
function tiiQuigoSetEnabled(b) {
	_tiiQuigoEnabled = b;
}

function tiiQuigoIsEnabled() {
	if (typeof(_tiiQuigoEnabled) == "boolean") {
		return _tiiQuigoEnabled;
	}
	return true;
}

function tiiQuigoWriteAd(pid, placementId, zw, zh, ps) {
	if (tiiQuigoIsEnabled()) {
		qas_writeAd(placementId, pid, ps, zw, zh, 'ads.adsonar.com');
	}
}



// Ad Tag Migration
var adConfig = new TiiAdConfig("3475.tim"); 
adConfig.setCmSitename("cm.tim");

// For Tacoda
var tcdacmd="dt";
adConfig.setTacodaTracking(false);

// Interstitials and popups
function paramExists(param) { 
        if(document.location.search.indexOf(param)>-1){
          return true;
        } else {
          return false;
        }
         
}

//For revenue Science
adConfig.setRevSciTracking(true);
if ( document.referrer.indexOf("cnn")>=0 || document.referrer.indexOf("google")>=0 || paramExists("partner") || paramExists("no_popup") ) {
	adConfig.setPopups(false);
}

function attachScript(src)
  {
    var script = document.createElement('script');
    script.type = 'text/javascript';
    script.setAttribute('defer','defer');
    document.getElementsByTagName('head')[0].appendChild(script);
    script.src = src;
  }



(function($) {
  var clearValue = function(){
    this.value='';
  };

  $.elementReady('query', function(){
    $(this).one('click',clearValue);
  });
  
  $.elementReady('query2', function(){
    $(this).one('click',clearValue);
    });


  $(function() {
    var $videoTools = $('#videoTools');
  
  	$videoTools.find('.emailThis a').timePopUp({height : 730, width: 750});	
  	$videoTools.find('.digg a').timePopUp({height : 450, width: 970});	
  	$videoTools.find('.facebook a').timePopUp({height : 436, width: 626});	
  
  });
  
$.extend({
  getUrlVars: function(){
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
      hash = hashes[i].split('=');
      vars.push(hash[0]);
      vars[hash[0]] = hash[1];
    }
    return vars;
  },
  getUrlVar: function(q){
    return $.getUrlVars()[q];
  }
});
	
var qval = $.getUrlVar('q').toLowerCase();	
qval = qval.replace(/[\W_]/g,'');
if (qval == "gamechangers" || qval == "gamechanger"){
	window.location.href='http://www.time.com/time/specials/packages/0,28757,2091589,00.html';	
}
else if (qval == "personoftheyear" || qval == "poy"){
	window.location='http://www.time.com/poy';	
}
else {
}
})(jQuery);
