jQuery.noConflict();
insidesource = {};
(function($) {
	//drop down menu for hotspots
	insidesource.setupHotspots = function() {
		var self = this;
		var trigger = $("#hotspots");
		var ul = $("ul", this);
		$("#hotspots, #subnav li ul").hover(function(){
			
				$("#subnav li ul").toggle();
				trigger.toggleClass('selected');
		
		});
	};

  insidesource.setupSlideshow = function () {
    if ($('.full .slideshow').length != 0) {
      $('.full .slideshow').cycle({
        fx: 'fade',
        timeout: 7000,
        speed: 'slow',
        next:   '.next_feature',
        prev:   '.previous_feature',
        pager: '#slideshow_pager',
				pause: 'true'
      });
    }
    var $insider_slideshow = $('#related_modules .insiders .slideshow');
    if ($insider_slideshow.length != 0) {				
			var curInsider = 0;
			
			$.get('/view/page.standard/insider?insider='+insiders[0], function(data) {
		  	$('li.insider_slide').html(data);
			});	
			$('.insiders .slideshow_nav a').click(function(e) {
				e.preventDefault();
				if (this.className == 'next')
					curInsider++;
				else
					curInsider--
				$.get('/view/page.standard/insider?insider='+insiders[curInsider], function(data) {
			  	$('li.insider_slide').html(data);
			 		insidesource.recordOutboundLink('Insider Links', insiders[curInsider]);
				});		
			})      
    }

    if ($('#related_modules .guide .slideshow').length != 0) {
      $('#related_modules .guide .slideshow').each(function() {
        var $this = $(this);
        var updatePage = function(curr, next, opts) {
          $(curr).parents('.guide').find('.page_number').text(opts.currSlide + 1);
					if ($('#related_modules .guide .slideshow .product_caption').length != 0) {
						$(curr).parents('.bd').addClass('with-caption');	
					}
        };
        $(this).cycle({
          fx: 'scrollHorz',
          easing: 'swing',
          timeout: 0,
          speed: '400',
          next: $this.parents('.guide').find('.next_gift'),
          prev: $this.parents('.guide').find('.previous_gift'),
          after: updatePage
        });
      });
    }
    if ($('#main .related_posts .slideshow').length != 0) {
      $('#main .related_posts .slideshow').each(function() {
        var $this = $(this);
        $(this).cycle({
          fx: 'scrollHorz',
          easing: 'swing',
          timeout: 0,
          speed: '400',
          next: $this.parents('.related_posts').find('.next_posts'),
          prev: $this.parents('.related_posts').find('.prev_posts')
        });
      });
    }
  };


  insidesource.setupArchiveFilters = function () {
    $('a.filter_header').click(function(){
      $(this).next().toggle();

      $('div.filters').each(function(){
        var showScroll = false;
        $(this).find('ul').each(function(){if ($(this).height() > 230) showScroll = true;});
        if (showScroll) {
          $(this).css('height',280);
          $(this).css('width',228);
          $(this).jScrollPane({showArrows:true});
          $('.jScrollArrowDown,.jScrollArrowUp').css('width',228)

        }
      });
      return false;
    });
  };

  insidesource.setUpTabs = function () {
		if ($('.tabs_nav').length == 0) return;
    $('.pane.popular').css('display','block');
    $('#picks').parent().addClass('inactive');

    $("#picks").click(function(event) {
      event.preventDefault();
      if ($('#picks').parent().hasClass('inactive')) {
        $('#picks').parent().toggleClass("inactive");
        $('#popular').parent().toggleClass("inactive");
        Cufon.refresh('.neutra');
        $('.picks').show();
        $('.popular').hide();
      } else {
        return false;
      }
    });

    $("#popular").click(function(event) {
      event.preventDefault();
      if ($('#popular').parent().hasClass('inactive')) {
        $('#popular').parent().toggleClass("inactive");
        $('#picks').parent().toggleClass("inactive");
        Cufon.refresh('.neutra');
        $('.popular').show();
        $('.picks').hide();
      } else {
        return false;
      }
    });
  };

  insidesource.setupTwitterticker = function () {
		if ($('#twitter').length == 0) return;
		$("#twitter").twitterTicker({
			userName: "TheInsideSource"
		});
  };

  insidesource.setupForms = function () {
    if ($('form').length == 0) return;
    //focus states for input/text fields
    $('input[type="text"],input[type="password"]').focus(function() {
		  $(this).addClass("focused");
      if (this.value == this.defaultValue){
        this.value = '';}
          if(this.value != this.defaultValue){
            this.select();
          }
		});
		$('input[type="text"],input[type="password"]').blur(function() {
      $(this).removeClass("focused");
        if ($.trim(this.value) == ''){
          this.value = (this.defaultValue ? this.defaultValue : '');
      }
    });
  };

  insidesource.setupNewsletter = function () {
    if ($('.signUp').length == 0 ) return;

    function clearForm(form) {
      $(form).find("input.text").attr('value',"Email Address");
      $(form).find("input:checkbox").attr('checked',"");
    }

    function success(form) {
			_gaq.push(['_trackEvent', 'Forms', 'Newsletter']);
      $(form).prevAll(".success").fadeIn().delay(5000).fadeOut();
      clearForm(form);
      return false;
    }

    function error(data,form) {      

      $(form).prevAll(".failure").fadeIn().delay(9000).fadeOut();
      clearForm(form);
      return false;
    }

    function postToList(form) {
      //var attributes = $(form).find('input.email').serializeArray();
      var email = $(form).find('input.email').val();
      $.post(
	      "/view/page.standard/yesmailform",
	      'email='+escape(email),
	      function(data) {
          if (! /"FAILURE"/.test(data) ) {
            success(form);
          } else {
            error(data,form);
          }
	      }
      );
    }

    function yesmailListId(form) {
      //code from yesList to add list id
      var checkboxes = document.getElementsByName("mappingListsCheckBox");
        var list_ids="";
        for(var i=0;i<checkboxes.length;i++){
            if(checkboxes[i].checked)
               list_ids+=","+checkboxes[i].value;
        }
        document.getElementById("list_ids_hidden").value=list_ids.substring(1);
        postToList($(this));
    }

    $(".signUp").submit(function() {
      var isValid = $(this).valid();
      if (isValid == true) {
        postToList($(this));
        return false;
      } else {
        return false;
      }
    });
  },

  insidesource.imageCollage = function() {	
      $('.collage_item').each(function() {$(this).hover(function() {$(this).find('.collage_detail').fadeIn('fast');},function() {$(this).find('.collage_detail').fadeOut('fast')});});
			$('.collage_container').each(function(){
				var container = $(this);
				$(container).find('#product_collage_image').load(function() { $(container).width($(this).width()+'px') });
			});
  },

	insidesource.recordOutboundLink = function(category, action, link) {
	  try {
			var acctID = _gaq._getAsyncTracker()._getAccount()  
	    var pageTracker=_gat._getTracker(acctID);
	    pageTracker._trackEvent(category, action);
	    if (link != null)
				setTimeout('document.location = "' + link.href + '"', 100)
	  }catch(err){}
	},

  insidesource.setupModals = function() {
		if ($('.modal').length == 0) return;

    $('.modal').livequery(function() {
      $(this).jqmAddClose('#close_modal');
			$(this).jqm({modal: true, trigger: 'a.email',overlayClass: 'overlay', overlay: 50 });
		});
    $('.close').livequery('click', function() {
			$('.modal').jqmHide();
			return true;
		});
	},
	
	insidesource.gallery = function() {
		var $gallery = $('.gallery .slideshow');
	  if ($gallery.length != 0) {
	      $gallery.each(function(index){
					$(this).cycle({
	        	fx: 'scrollHorz',
		        easing: 'swing',
		        timeout: 0,
		        speed: '400',
		        next: $(this).prev().find('.next'),
		        prev: $(this).prev().find('.previous')
		      });
			});
  	}
	},
    
	insidesource.shareThisTracking = function() {
		$('.connect_links span').click(function() {
			var spanClass = $(this).attr('class');
				if (spanClass == 'st_email') {
					insidesource.recordOutboundLink('ShareThisTIS', 'Email');	
				} else if (spanClass == 'st_twitter') {
					insidesource.recordOutboundLink('ShareThisTIS', 'Twitter');	
				} else if (spanClass == 'st_sharethis') {
					insidesource.recordOutboundLink('ShareThisTIS', 'Share');
				}			
		})
	},

	//call scripts on doc ready
	$(document).ready(function() {
    insidesource.setupHotspots();
    insidesource.setupSlideshow();
    insidesource.setUpTabs();
    insidesource.setupTwitterticker();
    insidesource.setupArchiveFilters();
    insidesource.setupNewsletter();
    insidesource.setupForms();
    insidesource.setupModals();
    insidesource.imageCollage();
		insidesource.gallery();
		insidesource.shareThisTracking();
    $('#scroll_tags').jScrollPane({scrollbarOnLeft:true, scrollbarWidth: 6});
    Cufon.replace(".austin", {fontFamily: 'austin', separate: 'none'});
    Cufon.replace(".neutra", {fontFamily: 'neutra', separate: 'none'});
    Cufon.replace("ul.pages li a,ul.pages li span", {fontFamily: 'neutra', separate: 'none', hover: true});
	});


})(jQuery);
