function getCookie(name) {
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
      if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  }
  return null;
}

function initializeTrackingHomePage() {
  jQuery('a#see_all_mixits').click(function(e) {
    trackMetrics({type: "mixit-page-interaction", data: "SeeAllMixits_link"});
  });
  jQuery('a#adventure_time').click(function(e) {
    trackMetrics({type: "mixit-page-interaction", data: "template_adventure_time"});
  });
  jQuery('a#battle_btn').click(function(e) {
    trackMetrics({type: "mixit-page-interaction", data: "template_battle_btn"});
  });
  jQuery('a#top5_btn').click(function(e) {
    trackMetrics({type: "mixit-page-interaction", data: "template_top5_btn"});
  });  
  jQuery('a#scratch_btn').click(function(e) {
    trackMetrics({type: "mixit-page-interaction", data: "StartFromScratch_btn"});
  });
  initializeTracking();
}

function initializeTracking() {
  jQuery('a#watch_btn').click(function(e) {
    trackMetrics({type: "mixit-page-interaction", data: "Watch_Landing"});
  });
  jQuery('a#mixit_btn').click(function(e) {
    trackMetrics({type: "mixit-page-interaction", data: "Mixit_Landing"});
  });
  jQuery('a#make_btn').click(function(e) {
    trackMetrics({type: "mixit-page-interaction", data: "Make_Landing"});
  });

}


function writeRelatedBumps(cache_base_url, base_url, num_bumps_for_related, num_bumps_for_recent, theme_slug, omit_bump){
  jQuery.getJSON(cache_base_url+"/"+theme_slug+"_related.json", {}, function(data) {
      var random_bumps = getRandomBumps(data.bumps, data.bump_size, num_bumps_for_related, omit_bump);
      var i = 0;
      jQuery.each(random_bumps, function(index, array) {
        writeRelatedBump(base_url, i, data.bumps[array]["bump_id"], data.bumps[array]["title"], data.bumps[array]["nice"], data.bumps[array]["author"], data.bumps[array]["url"], data.bumps[array]["thumbnail_path"], data.bumps[array]["views"], data.bumps[array]["theme_slug"], data.bumps[array]["theme_name"]);
        i++;
      });
  });
  //jQuery.getJSON(base_url+"/json_bumps_related.jsp", {}, function(data) {
  jQuery.getJSON(cache_base_url+"/json_bumps_related.json", {}, function(data) {
      var random_bumps = getRandomBumps(data.bumps, data.bump_size, num_bumps_for_recent, omit_bump);
      var i = 0;
      jQuery.each(random_bumps, function(index, array) {
        writeRecentBump(base_url, i, data.bumps[array]["bump_id"], data.bumps[array]["title"], data.bumps[array]["nice"], data.bumps[array]["author"], data.bumps[array]["url"], data.bumps[array]["thumbnail_path"], data.bumps[array]["views"], data.bumps[array]["theme_slug"], data.bumps[array]["theme_name"]);
        i++;
      });
  });
}

// write recent bump 
function writeRecentBump(base_url, li_id, bump_id, title, nice, author, url, thumbnail_path, views, theme_slug, theme_name) {
  jQuery("#most_recent_"+li_id).html('<div class="more_video_container"><a href="'+url+'"><img src="'+thumbnail_path+'" alt="'+title+'" width="144" height="81" /></a><div class="info"><h4><a href="'+url+'">'+title+'</a></h4><p class="category"><a href="'+base_url+'/gallery/show/'+theme_slug+'">'+theme_name+'</a></p><p class="username">by <a href="'+base_url+'/gallery/'+author+'">'+author+'</a></p><p class="stats">Views: '+views+' | <span>'+nice+'</span> </p></div></div></div>');
}

// write related bump 
function writeRelatedBump(base_url, li_id, bump_id, title, nice, author, url, thumbnail_path, views, theme_slug, theme_name) {
  jQuery("#related_bump_"+li_id).html('<div class="more_video_container"><a href="'+url+'"><img src="'+thumbnail_path+'" alt="'+title+'" width="144" height="81" /></a><div class="details"><div class="info"><h4><a href="'+url+'">'+title+'</a></h4><p class="category"><a href="'+base_url+'/gallery/show/'+theme_slug+'">'+theme_name+'</a></p><p class="username">by <a href="'+base_url+'/gallery/'+author+'">'+author+'</a></p><p class="stats">Views: '+views+' | <span>'+nice+'</span> </p></div></div></div>');
}

// get array of randomly selected bumps with no dupes
function getRandomBumps(bumps, array_size, num_bumps, omit_bump) {
  var i = 0;
  var random_bumps = new Array();
  while (i < num_bumps) {
    var random_int = Math.floor(Math.random()*array_size+1);
    while ((random_bumps.length <  array_size && omit_bump != bumps[random_int]["bump_id"]) && jQuery.inArray(random_int, random_bumps) != -1 ) { // remove dupe and page's bump
      random_int = Math.floor(Math.random()*array_size+1);
    }
    random_bumps.push(random_int);
    i++;
  }
  return random_bumps;
}

function fb_pop(inURL) {
  shareWindow = window.open(inURL,"shareWindow", "status=1,location=1,resizable=1,scrollbars=0,width=650,height=550");
  shareWindow.moveTo(180,170);
}
