/*raw element extenders*/
Array.prototype.max = function(){return Math.max.apply({},this)};
/* END:raw element extenders*/
 
 pageInfo={
  iframe: (window.location.toString().indexOf("iframe")!=-1)
 }
 
 var gh_ui,
    gh_mi,
    gh_gpb,
    cm_w,
    cm;
  
  
if(!gh_ui){gh_ui={};}
if(!gh_gpb){gh_gpb={};}
if(!gh_mi){gh_mi={};}

if(!cm_w){cm_w={};}
if(!cm){cm={};}

 
/*Global variables*/
gh_gpb.res = {};
gh_gpb.cnst = {};
(function(){
var s = gh_gpb.res;
var c = gh_gpb.cnst;
 
c.chrlmt = 160;
/*text string*/
s.pre = "Submit your entry here...";
s.inlmt = " characters remaining";
s.outlmt = "(You can not write more than # characters!)";
s.more = "more";
s.less = "less";
/*END:text string*/
})();
/*END:Global variables*/
 
(function(){
	function swapElementsForWidgets()
	{
		var prz = $("#prize");
		var ei = $("#endsIn");
		var hdn = $("#tc_content h2");
		var dsn = $("#tc_content h3");
		var ec = $(".expandedContestContent");
		var edi   = $(".entryDetailsImage");
		var icn = $(".to .cx b");
		
		prz.insertAfter(hdn);
		ei.insertAfter(dsn);

		makeRulesSlide();

	
    ec.each(function(){
        var x = $(this);
        var y = x.siblings(".submittedBy");
        x.insertAfter(y);
    });
    
    edi.insertAfter($(".submittedBy"));
    
    icn.click(function(){
      window.open($(this).siblings("a").attr("href"),'sharer','toolbar=0,status=0');
    });

    if ($.browser.msie && $.browser.version=="6.0")
    {
      fixPng();
      fixIcons();
    }
	}	

		function makeRulesSlide()
		{
      var ht = $("#howTo");
		  var cr = $("<div id='cr_toggle'><a href='javascript:;'>contest rules</a></div>")
  		cr.insertBefore(ht);
		  var crt = $("#cr_toggle a");
		  crt.click(function(){ht.slideToggle();});
		}


    function left(string, sym) { return string.substring(0, string.indexOf(sym)); }
    function right(string, sym) { return string.substring(string.indexOf(sym) + 1); }


    function fixIcons()
    {
        var targets = $("#why li span,#m_itdn h1,.gchdrf h1,.exp .left h1,.exp .right h1");
        targets.each(function(){
            var that = $(this);
            var x = that.css("background-position");
            var bgp = (x)?x:(that.css("background-position-x") + " " +that.css("background-position-y"));
            
	        var bi = that.css("background-image");
            var i = {};
            
            i.left = left(bgp.replace(/px/g,"")," ");
            i.top = right(bgp.replace(/px/g,"")," ");
            i.url = bi.replace(/url\((.*)\)/,"$1").replace(/"/g,"");

            that.psn = that.css("position");
            that.l = that.css("left");
            that.t = that.css("top");
            that.dp = that.css("display");
            that.w = that.css("width");
            that.h = that.css("height");
            
            that.css({
                position:(that.psn=="static")?"relative":that.psn,
                left:(that.l!=0)?that.l:0,
                top:(that.t!=0)?that.t:0,
                padding:0,
                overflow:"hidden",
                display:(that.dp!="inline")?that.dp:"inline-block",
                height:(that.h!="auto")?that.h:that.innerHeight(),
                width:(that.w!="auto")?that.w:that.innerWidth()
            });
            
            that.css({background:"none"});

            var alpha = "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + i.url +  "',sizingMethod='image');";
            
            var it = "<img src='images/clear.gif' style=\"" + alpha + "position:absolute;left:"+ i.left + "px;top:" + i.top + "px;\" >";
            
            that.html(that.html() + it);
        });
    }
	
    function fixPng()
    {
        var clear="img/clear.gif" //path to clear.gif
        var els = $("#p_wh a,#p_wf a");
        var ip=/\.png/i;

        els.each(function(){
            var el=this;
            var es=el.style;
            if(el.src&&el.src.match(ip)&&!es.filter)
            {
                es.height=el.height;
                es.width=el.width;
                es.filter="progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+el.src+"',sizingMethod='crop')";
                el.src=clear;
            }
            else
            {
                var elb=el.currentStyle.backgroundImage;
                if(elb.match(ip))
                {
                    var path=elb.split('"');
                    var rep=(el.currentStyle.backgroundRepeat=='no-repeat')?'crop':'scale';
                    es.filter="progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+path[1]+"',sizingMethod='"+rep+"')";
                    es.height=el.clientHeight+'px';
                    es.backgroundImage='none';
                    /*
                    var elkids=el.getElementsByTagName('*');
                    if (elkids)
                    {
                        var j=elkids.length;
                        if(el.currentStyle.position!="absolute")es.position='static';
                        while (j-- >0)if(!elkids[j].style.position)elkids[j].style.position="relative";
                    }
                    */
                }
            }
        });
    }


    cm_w.fic = fixIcons;
    cm_w.fp = fixPng;	
	cm_w.sefw = swapElementsForWidgets;
	cm.mrs = makeRulesSlide;
})();

(function(){
  function heightEqualizer(els)
  {
    var hs = [];
    for (var i=0;i<els.length;i++){hs.push($("#" + els[i]).height());}
    var max = hs.max();
    for (var i=0;i<els.length;i++){$("#" + els[i]).css({"min-height":max});}
  }
  function charCounter(arg)
  {
    var t=arg.trgt;
    var d=arg.dsply;
    var l=arg.chrlmt;
    var m=gh_gpb.res;
    var c=gh_gpb.cnst;
    
    var fm = "";
    var lmt=l?l:c.chrlmt;
    
    m.outlmt = m.outlmt.replace("#",lmt);
    
    t.keypress(function(){
      var l = t.val().length;
      if( l>lmt)
      {
        t.val(t.val().substring(0,lmt));
        fm = m.outlmt;
        show(fm);
        return false;
      }
      else
      {
        fm = "(" + (lmt - l) + m.inlmt + ")";
        show(fm);
      }
      
      function show(fm)
      {
        d.html(fm);
      }
    });
  }
  function centerElement(elmt)
  {
    elmt.css(
    {
      marginTop:-(elmt.height()/2),
      marginLeft:-(elmt.width()/2),
      position:"absolute",
      left:"50%",
      top:"50%"
    })
    
  }
  
  gh_ui.ce = centerElement;
  gh_ui.cctr = charCounter;
  gh_ui.he = heightEqualizer;
})();
 
(function(){
  function initializeContestSubmit()
  {
    var arg=arguments;
    
    var m = gh_gpb.res;
    var t = $("#entryText");
    var d = $("#cs_chars");
    var cs_obj = {trgt:t,dsply:d};
    t.focus(function(){
      t.addClass("active");
      if (t.val()==m.pre)
      {
        t.val("");
      }
    });
    t.blur(function(){
      t.removeClass("active");
      if (t.val()=="")
      {
        t.val(m.pre);
      }
    });
  gh_ui.cctr(cs_obj);
  }
  
  function initializeContestEntryExpando()
  {
    var m = gh_gpb.res;
    var Es = $(".contestEntry");
    Es.each(function(){
      var e = $(this);
      var mr = e.find(".ce_more");
      var ec = e.find(".expandedContestContent");
      var cn = e.find(".m_contestEntry");
      
      mr.click(function(){
        var l=$(this);
        if(l.html()==m.more){l.html(m.less);}else{l.html(m.more);}
        ec.slideToggle();
      });
    });
  }
  
  function initializeMediaContestEntry()
  {
    var cons = $(".mce .mce_content");
    cons.each(function(){
      var con = $(this);
      
      var imgCon = con.find(".mce_media")
      var img = imgCon.find("img");
      
      img.load(positionImage);
      
      function positionImage()
      {
        if (pageInfo.iframe)
        {
          if (img.width()>imgCon.width())
          {
            img.css({width:imgCon.width()});
          }
        }
        if (imgCon.height()>img.height())
        {
          gh_ui.ce(img);
        }
      }
      
      
      
    });
  }
  
  function initializeShareContest()
  {
  	var node = $("#shareContest");
  	var fb = node.find(".fb");
  	fb.click(openFacebook);
  	var tw = node.find(".tw");
  	tw.click(twitThis);
  	var dig = node.find(".dig");
  	dig.click(openDigg);
  	var dlco = node.find(".dlco");
  	dlco.click(openDelicious);
  	var su = node.find(".su");
  	su.click(openStumble);
  	var ms = node.find(".ms");
  	ms.click(openMyspace);
  }
  
  gh_mi.mce = initializeMediaContestEntry;
  gh_mi.cee = initializeContestEntryExpando;
  gh_mi.cs = initializeContestSubmit;
  gh_mi.sc = initializeShareContest;
})();



function openFacebook() {
  u=location.href;
  t=document.title;
  window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent(u)+
    '&t='+encodeURIComponent(t),'sharer','toolbar=0,status=0,width=626,height=436');
  return false;
}
function twitThis() {
  u=location.href;
  t=document.title;
  window.open('http://twitthis.com/twit?url='+encodeURIComponent(u)+
    '&t='+(encodeURIComponent(t) ? encodeURIComponent(t.replace(/^\s*|\s*$/g,'')) : ""),'TwitThis','width=600, height=500, location, status, scrollbars, resizable');
  return false;
}
function openDigg() {
  u=location.href;
  t=document.title;
  window.open('http://digg.com/submit?url='+encodeURIComponent(u)+
    '&t='+encodeURIComponent(t),'sharer','toolbar=0,status=0');
  return false;
}
function openDelicious() {
  u=location.href;
  t=document.title;
  window.open('http://del.icio.us/post?url='+encodeURIComponent(u)+
    '&t='+encodeURIComponent(t),'sharer','toolbar=0,status=0');
  return false;
}
function openStumble() {
  u=location.href;
  t=document.title;
  window.open('http://www.stumbleupon.com/submit?url='+encodeURIComponent(u)+
    '&t='+encodeURIComponent(t),'sharer','toolbar=0,status=0');
  return false;
}
function openMyspace() {
    u=location.href;
    t=document.title;
    window.open('http://www.myspace.com/index.cfm?fuseaction=postto&' + 't=' +encodeURIComponent(t)+
      '&c='+encodeURIComponent(t)+ '&u='+encodeURIComponent(u),'sharer','toolbar=0,status=0');
    return false;
}

