jQuery(document).ready(function($) {

       var tableId;
       var hostId;
       var supportId;

	//Control Piracy Form
	if($('.piracy table tr td input').attr('class') == 'textbox') 
	{
	  $('.piracy').hide();
	} else {
	  $('a.report').hide();
	}
	
	//Control Bug Report Form
	//if($('.bugForm select').attr('class') == 'dropnopad')
	//{
	  //$('.bugForm').hide();
	//}
	
	$('.productInfo').click(function(){
	  $('.white_content #media').html("<div id=\"changeLogContent\">" + $(this).next().html() + "</div>");
	  $('.white_content').show();
	  $('#fade').show();
	});
	$('a.closeModal').click(function(){
	  $('.white_content').hide();
	  $('#fade').hide();
	});
	
	
	$('.resellerPopup').click(function(){
	  $('.white_content #media').html($('.resellerPopupContent').html());
	  $('.white_content').show();
	  $('#fade').show();
	});
	
	
	//Reseller Credit Popup
	$(".creditInfo").click(function() {
	  var html = $(this).next().html();
	  $("#moreInformation").html(html);
      
      if($("#moreInformation").is(":visible"))
      {
        $("#moreInformation").slideUp();
      }
      
	  $("#moreInformation").slideDown();
	});

	
     
      
	//Training Zone Tabs and Carousel
	 
	  $("ul.trainingzone").tabs("#panes > div");
	  $("#panes > .single:first").tinycarousel({display: 4, duration: 800, animation: true });
	  $("ul.trainingzone li").click(function(){
	    $("#panes > .single:visible").tinycarousel({display: 4, duration: 800, animation: true });
	  });

	
	$('.video-wrapper li').click(function() {
	  $('.video-wrapper li').removeClass('active');
	  $(this).addClass('active');
	  $("#featured-training").html("<img src=\"/images/ajax-loader.gif\" class=\"ajax-loader\"/>");
	  var videoId = $(this).children('input').val();
          var jqxhr = $.get("support.php", { getAjaxVideo: videoId }, function(data) {
   	    $("#featured-training").html(data);
  	  });
	});
	

	$('.report').click(function(){$('.piracy').slideDown('slow');});
	$('.bugdrop').click(function(){$('.bugForm').slideDown('slow');});
	
	
	//JQuery Accordion for Product Pages.
	
	$('.accordionButton').click(function() {
		$('.accordionButton img').attr("src", "/images/accordionPlus.png");
		$('.accordionButton').removeClass('on');
		$('.accordionID').removeClass('idon');
	 	$('.accordionContent').slideUp('normal');
		if($(this).next().is(':hidden') == true) {
			$(this).addClass('on');
			$(this).prev().addClass('idon');
			$(this).children("img").attr("src", "/images/accordionMinus.png");
			$(this).next().slideDown('normal');
		}
	 });
	$('.accordionButton').mouseover(function() {
		$(this).addClass('over');
		$(this).prev().addClass('idon');
		$(this).children("img").attr("src", "/images/accordionPlusHover.png");
	}).mouseout(function() {
		$(this).removeClass('over');
		$(this).prev().removeClass('idon');
		$(this).children("img").attr("src", "/images/accordionPlus.png");
		
		if($(this).next().is(':hidden') == false) {
			$(this).children("img").attr("src", "/images/accordionMinus.png");
			$(this).prev().addClass('idon');
		}
	});
	$('.accordionContent').hide();
	
	
	
	
	//Download operating system button
	
	$('.os').click(function() {
	  tableId = $(this).parents("table").attr("id");
	  var os = $('#'+tableId+' select').val();  
	  if(os == "") 
	  {
	    $('#'+tableId+' .os-error').html('You must select an operating system');
	  } 
	  else
	  {
	    window.location = "maintenance.php?download="+os;
	  }
	});
	
	// Download product 
	$('.operating-system').change(function(){
	  $(this).parent().next().children().attr('href','/maintenance.php?download=' + $(this).val());
	});
	
	
	
	


        //AJAX/Post Query to request license.
	$('.requestLicense').click(function() { 
	
	  tableId = $(this).parents("table").attr("id");
	  hostId = $('#'+tableId+' input').val();
   	  supportId = $("#"+tableId+" p.supportID").text();
	
	//Set up before and after effects on post request.
	jQuery.ajaxSetup({
          beforeSend: function() {
            $('#'+tableId+' .loader').css('display','block');
            $('#'+tableId+' .requestLicense').hide();
          },
          complete: function() {
            $('#'+tableId+' .loader').css('display','none');
            $('#'+tableId+' .requestLicense').show();
          }
        });

	if(hostId == "") 
	{
	  $('#'+tableId+' .host-error').html('You must enter a Host ID into the text field');
	} 
	else 
	{
	  $.post("maintenance.php", { requestLicense: "", hostIds: hostId, supportIds: supportId },
       	   function(data) {
       	    if(data == "THANKS") {
       	      $('#'+tableId+' tr').children('td:last').html("<img style=\"display:block; margin:0 0 10px 65px\" src=\"/images/Complete.png\" alt=\"Complete\"/><h2 style=\"line-height:24px\">Thankyou, your license has been sent to your email.</h2>");
       	    } 
       	    else 
       	    {
       	      $('#'+tableId+' .host-error').html(data);
            }  	
           });
        } 
        
	}); 
	


	//Set Default State of each portfolio piece
	$(".paging").show();
	$(".paging a:first").addClass("active");
		
	//Wait for all images to load so that we can get the proper dimensions.
	$(window).load(function(){
	  var imageWidth = $(".image_reel img").width();
	  var imageSum = $(".image_reel img").size();
	  var imageReelWidth = imageWidth * imageSum;
	
	  //Adjust the image reel to its new size
	  $(".image_reel").css({'width' : imageReelWidth});
	
	  //Paging + Slider Function
	  rotate = function(){	
		var triggerID = $active.attr("rel") - 1; //Get number of times to slide
		var image_reelPosition = triggerID * imageWidth; //Determines the distance the image reel needs to slide

		$(".paging a").removeClass('active'); //Remove all active class
		$active.addClass('active'); //Add active class (the $active is declared in the rotateSwitch function)
		
		//Slider Animation
		$(".image_reel").animate({ 
			left: -image_reelPosition
		}, 500 );
		
	}; 
	
	  //Rotation + Timing Event
	  rotateSwitch = function(){		
		play = setInterval(function(){ //Set timer - this will repeat itself every 3 seconds
			$active = $('.paging a.active').next();
			if ( $active.length === 0) { //If paging reaches the end...
				$active = $('.paging a:first'); //go back to first
			}
			rotate(); //Trigger the paging and slider function
		}, 5000); //Timer speed in milliseconds (5 seconds)
	};
	
	  rotateSwitch(); //Run function on launch
	
	  //On Hover
	  $(".image_reel a").hover(function() {
		clearInterval(play); //Stop the rotation
	}, function() {
		  rotateSwitch(); //Resume rotation
	  });	
	
	  //On Click
	  $(".paging a").click(function() {	
		$active = $(this); //Activate the clicked paging
		//Reset Timer
		clearInterval(play); //Stop the rotation
		rotate(); //Trigger rotation immediately
		rotateSwitch(); // Resume rotation
		return false; //Prevent browser jump to link anchor
	});	
	});
	
	
	
	//-----Tool Tip Adjustments ------//
	var upX = 0;
	var upY = 10;
	
	var downX = 0;
	var downY = 10;
	
	jQuery.each(jQuery.browser, function(i, val) {
	   if(i == 'opera' || i == 'msie') 
	   {
	     upX = -60;
	     upY = -10;  
	     downX = -70;
	     downY = 10;
	   }
   	});

	$(".info[title]").tooltip({
   		offset: [upY, upX],
  		effect: 'slide'
	}).dynamic({ bottom: { direction: 'down', offset:[downY,downX], bounce: true } });

});

