/**** Add application wide javascripts below this point  ******/
$(document).ready(function() {
  $(".vote_comment_up, .vote_comment_down").click(function(){
	  var clicked_el = $(this);
	  $.get($(this).attr("href"), function(response){
      clicked_el.parent().find(".vote_message").html(response);
      $.get("/comment_karma/"+clicked_el.attr("rel"), function(response){
        karma_el = clicked_el.parent().parent().find(".comment_karma");
        karma_el.fadeOut("slow");
  	    karma_el.html(response);
  	    karma_el.fadeIn("fast");
  	  });
	  });
	  mess_el = clicked_el.parent().find(".vote_message");
	  mess_el.css({backgroundColor: '#ff0'});   
	  mess_el.fadeTo("slow", 0.9);
    setTimeout(function() { mess_el.children().fadeOut("slow"); mess_el.fadeTo("slow", 0);}, 3000);
	  return false;
	});
	$(".comment").each(function(){
	  var karma = $(this).attr("rel");
	  var opac = 1 + ((karma * 20)/100);
	  if(opac < 0.3) opac = 0.3;
	  if(opac < 1) {
	    $(this).fadeTo("slow", opac);
    }
	});
	
  $(".home_article img").hide();
  $("#home_module .home_article").each(function(){
	  $(this).css("backgroundImage", "url("+$(this).find("img").attr("src")+")");
	});
	
	$("#features_module .home_article, #reviews_module .home_article").each(function(){
	  $(this).css("backgroundImage", "url("+$(this).find("img").attr("src")+")");
	});
	$(".home_article, #home_latest").hover(
	  function(){
	    $(this).addClass("home_article_active");
	    var url = $(this).find("a").attr("href");
	    if(!$.browser.msie) $(this).wrap("<a href='"+url+"'></a>");
	  }, 
	  function(){$(this).removeClass("home_article_active");
	});
	
	$(".login_register_link").click(function(){
	  $(this).modal();
	  $(this).modal_show();
	  $.get("/login",function(response){
	    $("#modal_content").append(response);
	    setup_login();
	  });
	  return false;
	});
  
  if($.cookie("news_ticker")=="hide") {
    $("#ticker").hide();
  }
  
  
  $(".tabbed_module .module_nav").tabs();
	$(".ticker_close a").click(function(){$("#ticker").fadeOut("slow"); return false; });
	
	
	
	
	$("#cms_content_body .article_image").before("<hr />");

	
	if($("#flowplayer").length) flowplayer("flowplayer", "http://media.strangeglue.com/flowplayer/flowplayer-3.1.4.swf");
  
});



$(window).load(function () {
  
	
	/***** Image Gallery ********/
  $(".scroll_image_gallery").jCarouselLite({
      btnNext: ".next_button",
      btnPrev: ".previous_button",
      visible: 1,
      auto: 8000
    });
  
  
  
  $(".gallery_preview").parent().hover(function(){
    $(this).prepend("<div class='gallery_overlay'>"+$(this).attr("title")+" ...Click to see more</div>");
    $(".gallery_overlay").fadeTo("slow", 0.7);
  },
  function(){
    $(".gallery_overlay").fadeOut("fast", function(){$(this).remove(); });
  });
  
  $(".page_gallery_preview").parent().hover(function(){
    $(this).prepend("<div class='page_gallery_overlay'>"+$(this).children("img").attr("title")+"</div>");
    $(".page_gallery_overlay").fadeTo("slow", 0.7);
  },
  function(){
    $(".page_gallery_overlay").fadeOut("fast", function(){$(this).remove(); });
  });
	
  $(".page_gallery").slideView();
  
  $(".page_gallery").each(function(){
    var maxheight=0;
    $(this).find("li").each(function(){
      if($(this).height() > maxheight) maxheight= $(this).height();
    });
    $(this).css("height", maxheight);
  });
  
  
});



$(document).ready(function(){
  $("#site_search input").example('Search the site');
    
});

function setup_login() {
  $("#login_form form, #register_form form").submit(function(){
    form_errors=0;
    $(this).find(".fields2 input[type=text],.fields2 input[type=password]").each(function(){
      if($(this).val().length < 2) { form_errors = form_errors+1; $(this).css("border", "1px solid red");}
    });
    if(form_errors > 0) {alert("All form fields are required"); return false;}
  });
}


/************ News Ticker ************************/

var news_items;
var current_news=0;
var news_pause=0;
/***** Homepage Ticker Rotator ************/
$(document).ready(function() {
  if(!ie6) {
    news_items = $("ul#ticker li").hide().filter(":eq(0)").show().add("ul#ticker li").size();
    if(news_items > 1) setInterval(auto_ad,7000);
    $("ul#ticker li").hover(function(){news_pause=1;}, function(){news_pause=0;});
  }
});

function auto_ad() {
  if (news_pause==0){
      $("ul#ticker li:eq("+current_news+")").fadeOut("slow",function(){
        $(this).hide();
        current_news = ++current_news%news_items;
        $("ul#ticker li:eq("+current_news+")").fadeIn("slow");
      });
    }
}



/************ End News Ticker ********************/

google_ad_client = "pub-3044692468316944";
google_ad_slot = "2207712764";
google_ad_width = 160;
google_ad_height = 600;

$(document).ready(function() {
  var pageTracker = _gat._getTracker("UA-627977-3");
    $('a[href*="media.strangeglue.com"]').click(function(){
      var pageTracker = _gat._getTracker("UA-627977-3");
     pageTracker._trackPageview($(this).attr("href"));
     return true;
   });
});




