
$(document).ready(function() {

	// check that user searches for a keyword
	$("#searchfrm").submit(function() {
		if ( $("input:first").val().length > 0 ) {
			return true;
		}
		$(".searcherror").text("Please enter a keyword to search for").show().fadeOut(5000);
		return false;
	});
						  
	// sidebar subnav toggle (if exist)
	$('.sidebar-nav > li > a').click(function(){
		if($(this).next().length > 0){
			$(this).next().toggle('fast');
			return false;
		}
	});
						  
	// toggle home content
	$('.home .sub-content-inner span.ir-title').click(function(){
		var currentClass = 'feature-' + $(this).parents('.feature').attr('class').split(' ')[1];
		$('.home .sub-content-inner')
			.removeClass('feature-digital-signage feature-corporate-screensavers feature-in-store-radio')
			.addClass(currentClass);			
	});
	
	// toggle marketing/IT info on home
	$('.title ul li').click(function(){
        var type = "0";
        
        if($(this).hasClass('marketing'))
        {
            type = "1";   
        }
        else if($(this).hasClass('it'))
        {
            type = "2";   
        } 

        if(type == "1")
	    {
            if(!$(this).hasClass('marketing-current'))
            {
                $(this).removeClass('marketing');
                $(this).addClass('marketing-current');            
                $(this).prev().removeClass('it-current');
                $(this).prev().addClass('it');
                $(this).parent().parent().parent().find('.content').children('.marketing-info').show();            
                $(this).parent().parent().parent().find('.content').children('.it-info').hide();
                var w = $(this).parent().parent().parent().find('.content').children('.marketing-info').find('.more-link').attr('href');
                $(this).parent().parent().find('.product-logo, .product-main').attr('href', w);      
            }
        }
        else if (type == "2")
        {
            if(!$(this).hasClass('it-current'))
            {
                $(this).next().removeClass('marketing-current');
                $(this).next().addClass('marketing');
                $(this).removeClass('it');
                $(this).addClass('it-current');            
                $(this).parent().parent().parent().find('.content').children('.marketing-info').hide();            
                $(this).parent().parent().parent().find('.content').children('.it-info').show();
                var w = $(this).parent().parent().parent().find('.content').children('.it-info').find('.more-link').attr('href');
                $(this).parent().parent().find('.product-logo, .product-main').attr('href', w);
            }
        }
        
	});
	
	// toggle list benefits
	$('.list-benefits h2').click(function(){
		if ($(this).parent().parent().hasClass('expanded')) {
			$(this).parent().parent().removeClass('expanded');
			$(this).parent().parent().addClass('collapsed');	
		} else {
			$(this).parent().parent().removeClass('collapsed');
			$(this).parent().parent().addClass('expanded');	
		}
	});
		
	// hover on homepage
	$(".ir-title").hover(
      	function () {
        	$(this).addClass('ir-hover');
      	}, 
      	function () {
        	$(this).removeClass('ir-hover');
      	}
    );
	
	// hover on support-landing page
	$(".list-support li").hover(
      	function () {
        	$(this).addClass('hover');
      	}, 
      	function () {
        	$(this).removeClass('hover');
      	}
    );
		
	// tabs
	$('.tab-content').hide();
	$('.tab-active').show();
	$('.tab-control a').click(function(){
		$('.tab-control a').removeClass('current');
		$('.tab-control li').removeClass('current');
		$('.tab-content').hide();
		$($(this).attr('href')).show();
		$(this).addClass('current');
		$(this).parent().addClass('current');
		return false;
	});
	
	var hash = window.location.hash;
	if ( hash == "#marketing" ) {
		$('.tab-control a').removeClass('current');
		$('.tab-control li').removeClass('current');
		$("#tc-it").hide();
		$("#tc-marketing").show();
		$("#mt-marketing").addClass("current");
		$("#mt-marketing").parent().addClass("current");
		return false;
	}
	
	// setup thickbox
	var lbGalleries = [];
	$('a.lightbox-link[rel]').each(function(){
		var galleryName = $(this).attr('rel');
		if($.inArray(galleryName,lbGalleries) < 0) lbGalleries.push(galleryName);
	});
	$.each(lbGalleries,function(i,galleryName){
		var i = 0;
		var curGalleries = $('a.lightbox-link[rel="'+galleryName+'"]');
		var curLength = curGalleries.length;
		
		curGalleries.each(function(){
			$(this).data('index',i);
			if(i>0) 
				$(this).data('prev',$(this).attr('rel')+'-'+(i-1)); 
			else
				$(this).data('prev','none');
				
			if(i<curLength-1)
				$(this).data('next',$(this).attr('rel')+'-'+(i+1));
			else
				$(this).data('next','none');
				
			$(this).addClass(galleryName+'-'+(i++));
		});
	});
	
	$('a.lightbox-link').click(function(){
		var width = 492;
		var height = 400;
		tb_show('',
			$(this).attr('href')+'?width='+width+'&height='+height,
			$(this).attr('rel'),
			$(this).data('next'),
			$(this).data('prev'));
		
		return false;
	});
  
  // add styles to first downloadable product listing
  $('.downloads .product-columns:first').addClass('product-columns-first');
  
  // video listing topic switcher
	$('.topics li a').click(function(){
    var rel = this.rel;
    $('.topics li a').removeClass('current');
    $(this).addClass('current');
    if (rel==""){
      $('.video-list div.section, .video-list .topicqs li').show();
    } else {
      $('.video-list div.section, .video-list .topicqs li').hide();
      $('.video-list div.' + rel + ', , .video-list .topicqs li.' + rel).show();
    }
    return false;
  });
  
  // blog link hijacker
  $('a[title="Blog"], a[title="Forums"], a[title="Quick Start Guides"], a[title="Library"]').attr('target', '_blank');
  
  // accordian content stuff
  $('div.accordian .mod_content *:not(.icon, .summary, .summary *)').hide();
  $('div.accordian .mod_content').prepend('<span class="icon collapsed"></span>');
  $('div.accordian .mod_content span.icon').click(function(){
    $(this).toggleClass('collapsed');
    $(this).parent().children('*:not(.icon, .summary, .summary *)').show();
  });
  
  // add a link action to the homepage client logos div
  $('#section-clients ul').attr('style','cursor:pointer').click(function(){
    location.href = '/digital-signage-software/clients/';
  });
  
  // validate the comment form when it is submitted
  if ($("#contact-form").length > 0) {
    $("#contact-form").validate();
    // prepend http:// to url field if required
    if ($('#url').length > 0){
      $('#url').blur(function(){
        var pre = 'http://';
        var url = this.value();
        if(url.indexOf(pre)=-1){
          var full = pre + this.value();
          this.value(full);
        }
      });
    }
  }


});

// line up the support page tabs edit icons
divids = "nada_193,nada_194,nada_195,nada_196,";
$(window).load(function(){
  if (typeof( window[ 'pageid' ] ) != "undefined" && pageid > 0 && divids != "") {
    // modify the editor button positions
    var dividarr = divids.substring(0,divids.length - 1).split(",");
    // get the starting control position
    var off = $("#" + dividarr[0] + "_ctrl").offset();
    // loop through the divids and shift their offset
    var lplus = 80;
    var tplus = -30;
    for(var i=0;i<dividarr.length;i++) {
      $("#" + dividarr[i] + "_ctrl").css({
        left: (off.left + ((i+1)*lplus) + 'px'),
        top: (off.top + (1*tplus) + 'px')
      });
    }
  }
});

// downloads confirmation redirect
var uri = "";
function delayer(){
  if(uri.length>0)
    window.location = uri;
}

