var shiftBlockId="contentWrapperBlock";

$(document).ready(
	function() {

	    InitializeFancyBox();

	    InitializeNavigation();

	    InitializeGalleryNav();

	    InitializeProjectTable();

	    InitializeContactFormNav();
	});

	function InitializeProjectTable() {
	    $('[name=delete]').click(function() {
	        $tr = $(this).closest('tr');
	        if ($tr.find('.IsDeleted').length > 0)
	            $tr.find('.IsDeleted').val('1');
	        $tr.fadeOut('slow', function() {
	            if ($(this).find('.Id').length <= 0)
	                $(this).remove();
	        }

				);
	    });

	    $(".IsDeleted,.Order,.Desc1,.Desc2,.Desc3,.Detail1,.Detail2,.Detail3,.File1,.File2,.File3").change(function() {
	        $(this).closest('tr').find('.Changed').val(1);
	    });

	    $('[name=addRow]').click(function() {
	        AddProjectsTableRow($(this).closest('table')).fadeIn('slow');
	    });

	    $(".Order").keypress(function(e) {
	        if (e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57)) {
	            return false;
	        }
	    });

	}
	
	function InitializeFancyBox() {

		/* Using custom settings */
/*	
		$("a#inline").fancybox({
			'hideOnContentClick': true,
			 'type': 'image' 
		});
*/
		/* Apply fancybox to multiple items */

	    $("a.image_link").fancybox({
	        'type'          :   'image',
	        'hideOnContentClick': true,
			'transitionIn'	:	'elastic',
			'transitionOut'	:	'elastic',
			'speedIn'		:	600, 
			'speedOut'		:	200, 
			'overlayShow'	:	true
		});
	}
	
	function InitializeGalleryNav()
	{
		var shiftMax=parseInt($('[name=shiftMax]').val());
		var shiftIndex=parseInt($('[name=shiftIndex]').val());
		
		if(shiftIndex <= 0)
			$("#prevButton").fadeOut(0);
					
		if(shiftIndex >= shiftMax)
			$("#nextButton").fadeOut(0);
				
		if ($("#nextButton"))
		$("#nextButton").click(function()
			{
				var shiftMax=parseInt($('[name=shiftMax]').val());
				var shiftIndex=parseInt($('[name=shiftIndex]').val());
				var shiftDist = parseInt($(".contentBlock").width()) + parseInt($(".contentBlockSpacer").width())
				
				if(shiftIndex >= shiftMax)
					return false;
					
				shiftIndex++;
				$('[name=shiftIndex]').val(shiftIndex);

				$("#"+shiftBlockId).animate({'left': -shiftDist*shiftIndex+"px"},500);
					
				if(shiftIndex <= 0)
					$("#prevButton").fadeOut("slow");
				else
					$("#prevButton").fadeIn("slow");
					
				if(shiftIndex >= shiftMax)
					$("#nextButton").fadeOut("slow");
				else
					$("#nextButton").fadeIn("slow");
					
				return false;
			})
		
	if ($("#prevButton"))
		$("#prevButton").click(function()
			{
				var shiftMax=parseInt($('[name=shiftMax]').val());
				var shiftIndex=parseInt($('[name=shiftIndex]').val());
				var shiftDist = parseInt($(".contentBlock").width()) + parseInt($(".contentBlockSpacer").width())
				
				if(shiftIndex <= 0)
					return false;
					
				shiftIndex--;
				$('[name=shiftIndex]').val(shiftIndex);

				$("#"+shiftBlockId).animate({'left': -shiftDist*shiftIndex+"px"},500);
				
				if(shiftIndex <= 0)
					$("#prevButton").fadeOut("slow");
				else
					$("#prevButton").fadeIn("slow");
					
				if(shiftIndex >= shiftMax)
					$("#nextButton").fadeOut("slow");
				else
					$("#nextButton").fadeIn("slow");
				
				return false;
			})
	}
	
	function InitializeNavigation()
	{
		// IE6 doesn't handle the fade effect very well - so we'll stick with
	  // the default non JavaScript version if that is the user's browser.
	  if ($.browser.msie && $.browser.version < 6) return;

	  if ($('#navigation'))
	  $('#navigation li')
		// remove the 'highlight' class from the li therefore stripping 
		// the :hover rule
		.removeClass('highlight')
		
		// within the context of the li element, find the a elements
		.find('a')
		
		// create our new span.hover and loop through anchor:
		.append('<span class="hover" />').each(function () {
		  
		  // cache a copy of the span, at the same time changing the opacity
		  // to zero in preparation of the page being loaded
		  var $span = $('> span.hover', this).css('opacity', 0);
		  
		  // when the user hovers in and out of the anchor
		  $(this).hover(function () {
			// on hover
			
			// stop any animations currently running, and fade to opacity: 1
			$span.stop().fadeTo(500, 1);
		  }, function () {
			// off hover
			
			// again, stop any animations currently running, and fade out
			$span.stop().fadeTo(500, 0);
		  });
		});

		if ($('#navigationPL'))
		$('#navigationPL li')
		// remove the 'highlight' class from the li therefore stripping 
		// the :hover rule
		.removeClass('highlight')

		// within the context of the li element, find the a elements
		.find('a')

		// create our new span.hover and loop through anchor:
		.append('<span class="hover" />').each(function() {

		    // cache a copy of the span, at the same time changing the opacity
		    // to zero in preparation of the page being loaded
		    var $span = $('> span.hover', this).css('opacity', 0);

		    // when the user hovers in and out of the anchor
		    $(this).hover(function() {
		        // on hover

		        // stop any animations currently running, and fade to opacity: 1
		        $span.stop().fadeTo(500, 1);
		    }, function() {
		        // off hover

		        // again, stop any animations currently running, and fade out
		        $span.stop().fadeTo(500, 0);
		    });
		});
	}

	function InitializeContactFormNav() {
	    if ($("#contactFormBtn_pl,#contactFormBtn_en"))
	        $("#contactFormBtn_pl,#contactFormBtn_en").click(function() {
	            $("#contactFormBtn_pl,#contactFormBtn_en").animate({ 'left': -64 + "px" }, 500);
	            $("#contactFormBlock").animate({ 'right': 0 + "px" }, 500);
	            var shiftDist = parseInt($(".contentBlock").width())
	            $("#" + shiftBlockId).animate({ 'left': -shiftDist + "px" }, 500);
	            return false;
	        })

	    if ($("#contactFormXBtn"))
	        $("#contactFormXBtn").click(function() {
	        $("#contactFormBtn_pl,#contactFormBtn_en").animate({ 'left': 0 + "px" }, 500);
	            $("#contactFormBlock").animate({ 'right': -490 + "px" }, 500);
	            $("#" + shiftBlockId).animate({ 'left': 0 + "px" }, 500);
	            return false;
	        })
	}

	var projectRowItems = ['IsDeleted', 'Order', 'Desc1', 'Desc2', 'Desc3', 'Detail1', 'Detail2', 'Detail3', 'File1', 'File2', 'File3'];

	var prevNewRowNum = 0;
	
	function AddProjectsTableRow(table) {
	    prevNewRowNum++;
	    var currentNum = prevNewRowNum;
	    var newRowID = 'NewRow' + currentNum;
	    var $tr = $(table).find('#NewRowTemplate').clone(true);
	    $tr.attr("id", newRowID);

	    for (var i = 0; i < projectRowItems.length; i++) {
	        $tr.find('#' + projectRowItems[i]).attr("name", newRowID + '_' + projectRowItems[i]);
	        $tr.find('#' + projectRowItems[i]).attr("id", newRowID + '_' + projectRowItems[i]);
	    }

	    $(table).append($tr);
	    return $tr;
	};
