$(function() {

	var $slider = $('#full-slider');
	var $sliderPanels = $slider.children('.slide-panel');

//	var $navWrap = $('<div id="full-slider-nav"></div>').appendTo($slider);
//	var $navLeft = $('<div id="full-slider-nav-left"></div>')
//			.appendTo($navWrap);
//	var $navRight = $('<div id="full-slider-nav-right"></div>').appendTo(
//			$navWrap);

  var $navRight = $('#full-slider-nav-right');
  var $navLeft =  $('#full-slider-nav-left');
  
	var currPanel = 2;

	$navLeft.click(function() {
		currPanel--;

		// check if the new panel value is too small
		if (currPanel < 0)
			currPanel = $sliderPanels.length - 1;

		slidePanel(currPanel, 'right');
	});

	$navRight.click(function() {
		currPanel++;
		// check if the new panel value is too big
		if (currPanel >= $sliderPanels.length)
			currPanel = 0;

		slidePanel(currPanel, 'left');
	});
 
	/*-------------------------------------------------------*/
	var interval;
/*	$("div#ourwork").load("files.php", function() {
/*		$('.projectclick').imgPreview({
			containerID : 'imgPreviewWithStyles',
			 Change srcAttr to rel: 
			srcAttr : 'http://www.schmidhauser-immo.ch/',
			imgCSS : {
				// Limit preview size:
				height : 300
			},
			// When container is shown:
			onShow : function(link) {
				$('<span>' + link.href + '</span>').appendTo(this);
			},
			// When container hides:
			onHide : function(link) {
				$('span', this).remove();
			}
		});
	});*/
//	$("div#theteam").load("files.php?cmd=theteam", function() {
//		$('ul#roundabout').roundabout();
//		// let's get this party started
//		interval = startAutoPlay();
//	});

	activeItem = $("#accordion li a:first");
	$(activeItem).addClass('active');

	$("#accordion li a").hover(function() {
		$(activeItem).animate({
			width : "50px"
		}, {
			duration : 300,
			queue : false
		});
		$(this).animate({
			width : "630px"
		}, {
			duration : 300,
			queue : false
		});
		activeItem = this;
	});

	/*-------------------------------------------------------*/
	$('#memberlogin').click(function() {
		$('#login_form').modal();
		return false;
	});

	$('#contactus').click(function() {
		
		if ( $('#contactus').html() == 'introduction' ) {
		  $("div#mainpageblock1").load("files.php?page=introduction", function() {
			     if (currPanel != 2) slidePanel(2, 'left');
			     $('#contactus').html('Contacting us');			  	
		  });			
		} else {
	  	$("div#mainpageblock1").load("files.php?page=contactus", function() {
			     if (currPanel != 2) slidePanel(2, 'left');
			     $('#contactus').html('introduction');			  	
	  	});			
		}

		return false;
	});

	/*-------------------------------------------------------*/
	$("#status > form")
			.submit(
					function() {

						// Hide 'Submit' Button
						$('#submit').hide();
						// Show Gif Spinning Rotator
						$('#ajax_loading').show();

						var serializedForm = $(this).serialize();
						$.post("admin/admin.php", serializedForm,
								function(data) {
									$("#adminpanelWrap").html(data);
									$("#adminpanelWrap").draggable({
										handle : 'ul.ui-tabs-nav'
									});
									// $("#adminpanelWrap").resizable();
									$("#adminpanelWrap").show();
									$('#submit').show();
									$('#ajax_loading').hide();
									$.modal.close();
								});
						$("#status")
								.ajaxError(
										function(event, xhr, ajaxOptions,
												errorThrown) {
											$('#login_response')
													.html(
															xhr.status
																	+ ' invalid username and/or password');
											$('#submit').show();
											$('#ajax_loading').hide();
										});

						return false;

					}); // end submit event
	/*-------------------------------------------------------*/

	$('#ourwork').click(function() {
		if (currPanel != 0)
			slidePanel(0, 'left');
		currPanel = 0;
	});

	$('#theteam').click(function() {
		if (currPanel != 1)
			slidePanel(1, 'left');
		currPanel = 1;
	});

	$('#aboutus').click(function() {
		if (currPanel != 2)
			slidePanel(2, 'left');
		currPanel = 2;
	});

	function slidePanel(newPanel, direction) {
		// define the offset of the slider obj, vis a vis the document
		var offsetLeft = $slider.offset().left;

		// offset required to hide the content off to the left / right
		var hideLeft = -1 * (offsetLeft + $slider.width());
		var hideRight = $(window).width() - offsetLeft;

		// change the current / next positions based on the direction of the
		// animation
		if (direction == 'left') {
			currPos = hideLeft;
			nextPos = hideRight;
		} else {
			currPos = hideRight;
			nextPos = hideLeft;
		}

		// slide out the current panel, then remove the active class
		$slider.children('.slide-panel.active').animate({
			left : currPos
		}, 500, function() {
			$(this).removeClass('active');
		});

		// slide in the next panel after adding the active class
		$($sliderPanels[newPanel]).css('left', nextPos).addClass('active')
				.animate({
					left : 0
				}, 500);
	}
});

