// JavaScript Document

$(function() {
    prepareGallery();
	
	// force reszing for people with huge screens
	$(window).resize(prepareGallery);

	// set up the ajax form submit
	var options = { 
       //target:        '#formOutput',   // target element(s) to be updated with server response 
        beforeSubmit:  beginSubmit,  // pre-submit callback 
        success:       showResponse,  // post-submit callback 
		dataType: 'JSON'
    }; 
	
	$('#adminForm').ajaxForm(options);
	
	// create the crypt value
	var pathname = 'http://www.sktextile.com';
	var date = new Date();
	$('input.crypt').val($.md5(pathname + 'x3489fjd239j1'));
	
	
	$('#home_link').click(function(){
		
		$('#aboutus_link').removeClass('active');
		$('#aboutus_link').parent().siblings().children().removeClass('active');
		$('#home2_link').addClass('active');
		$('.wrapper').stop().animate({"left" : "0px", "top" : "0px"}, {easing:"easeInOutCubic", duration:3000, complete:function(){
		
			$('div.next').show();
		
		} });
		
	});
	
	$('#home2_link').click(function(){
		
		$(this).addClass('active');
		$(this).parent().siblings().children().removeClass('active');
		$('.wrapper').stop().animate({"left" : "0px", "top" : "0px"}, {easing:"easeInOutCubic", duration:3000 , complete:setListeners} );
		
		
	});
	
	$('#aboutus_link').click(function(){
		
		$(this).addClass('active');
		$(this).parent().siblings().children().removeClass('active');
		$('.wrapper').stop().animate({"left" : "-2200px", "top" : "0px"}, {easing:"easeInOutCubic", duration:3000});
		hideGallery();
	});

	$('#products_link').click(function(){
		
		$(this).addClass('active');
		$(this).parent().siblings().children().removeClass('active');
		$('.wrapper').stop().animate({"left" : "-4400px", "top" : "0px"}, {easing:"easeInOutCubic", duration:3000});
		hideGallery();
	});
	
	$('#services_link').click(function(){
		
		$(this).addClass('active');
		$(this).parent().siblings().children().removeClass('active');
		$('.wrapper').stop().animate({"left" : "-6600px", "top" : "0px"}, {easing:"easeInOutCubic", duration:3000});
		hideGallery();
	});
	
	$('#project_link').click(function(){
		
		$(this).addClass('active');
		$(this).parent().siblings().children().removeClass('active');
		$('.wrapper').stop().animate({"left" : "-8800px", "top" : "0px"}, {easing:"easeInOutCubic", duration:3000});
		hideGallery();
	});
	
	$('#partner_link').click(function(){
		
		$(this).addClass('active');
		$(this).parent().siblings().children().removeClass('active');
		$('.wrapper').stop().animate({"left" : "-11000px", "top" : "0px"}, {easing:"easeInOutCubic", duration:3000});
		hideGallery();
	});
	
	
	$('#ordering_link').click(function(){
		
		$(this).addClass('active');
		$(this).parent().siblings().children().removeClass('active');
		$('.wrapper').stop().animate({"left" : "-13200px", "top" : "0px"}, {easing:"easeInOutCubic", duration:3000});
		hideGallery();
	});
	
	$('#corporate_link').click(function(){
		
		$(this).addClass('active');
		$(this).parent().siblings().children().removeClass('active');
		$('.wrapper').stop().animate({"left" : "-15400px", "top" : "0px"}, {easing:"easeInOutCubic", duration:3000});
		hideGallery();
	});
	
	// listen for the register task
	if(jQuery.url.param("task")) {
	
		$('.register_link').addClass('active');
		$('.register_link').parent().siblings().children().removeClass('active');
		$('.wrapper').stop().animate({"left" : "-17600px", "top" : "0px"}, {easing:"easeInOutCubic", duration:3000});
		hideGallery();
		
	}
	
	$('.register_link').click(function(){
		
		$(this).addClass('active');
		$(this).parent().siblings().children().removeClass('active');
		$('.wrapper').stop().animate({"left" : "-17600px", "top" : "0px"}, {easing:"easeInOutCubic", duration:3000});
		hideGallery();
	});
	
	$('#register_link_top').click(function(){
		
		$(this).addClass('active');
		$(this).parent().siblings().children().removeClass('active');
		$('.wrapper').stop().animate({"left" : "-17600px", "top" : "0px"}, {easing:"easeInOutCubic", duration:3000});
		hideGallery();
	});
	
	$('#press_link').click(function(){
		
		$(this).addClass('active');
		$(this).parent().siblings().children().removeClass('active');
		$('.wrapper').stop().animate({"left" : "-19800px", "top" : "0px"}, {easing:"easeInOutCubic", duration:3000});
		hideGallery();
	});

	

});


/* gallery functions */

function prepareGallery()
{
	
	setListeners();
	setPlusListeners();
	
	// set the image div positions
	$('.home_images div.active').css('left' , '70');
	
	
	// set the position of the second element
	//$('.home_images div.active').next().css('left' , document.body.clientWidth + 50);
	
	
	// then get the rest of them off the screen
	$('.home_images div.active').nextAll().css('left' , document.body.clientWidth + 50);
	
	// hide all the plus sign rollovers
	$('.home_images div.image div.overlay div.hidden_box div.title').css('opacity' , '0').hide();
	$('.home_images div.image div.overlay div.hidden_box div.caption').css('opacity' , '0').hide();
	
	
}


function setListeners()
{
	$('div.next').show();
	// reveal all
	$('.home_images div').show();
	
	// set the mouse over listeners
	//$('.home_images div.image div.overlay div.next_button').bind('mouseover' , handleNextMouseOver);
	//$('.home_images div.image div.overlay div.next_button').bind('mouseout' , handleNextMouseOut);
	$('.home_images div.image div.overlay div.next_button div.clickarea').bind('mouseup' , next);	
	
	//$('.home_images div.image div.overlay div.back_button').bind('mouseover' , handlePrevMouseOver);
	//$('.home_images div.image div.overlay div.back_button').bind('mouseout' , handlePrevMouseOut);
	$('.home_images div.image div.overlay div.back_button div.clickarea').bind('mouseup' , prev);
	
	$('.home_images div.image div.overlay div.hidden_box div.title').css('opacity' , '0').hide();
	$('.home_images div.image div.overlay div.hidden_box div.caption').css('opacity' , '0').hide();
	
	

}

function setPlusListeners()
{

	// now create listeners for all the plus signs to reveal the hidden divs
	$('.home_images div.image div.overlay div.hidden_box div.clickarea').bind('mouseover' , handlePlusRollover);
	$('.home_images div.image div.overlay div.hidden_box div.clickarea').bind('mouseout' , handlePlusRollout);
	$('.home_images div.image div.overlay div.hidden_box div.clickarea').toggle(handlePlusClickOpen , handlePlusClickClose);
	
}

function hideGallery()
{
	
	clearListeners();
	$('div.next').hide();
	$('.home_images div.next').nextAll().hide();
	$('.home_images div.prev').prevAll().hide();

}


function clearListeners()
{

	 // remove all event listeners
	 $('.home_images div.next').unbind().clearQueue().stop();
	 $('.home_images div.active').unbind().clearQueue().stop();
	 $('.home_images div.prev').unbind().clearQueue().stop();
	
}


function handleNextMouseOver()
{
	$(this).clearQueue();
	$(this).animate({opacity: 1} , 1000, '' , function() {});	
}

function handleNextMouseOut()
{
	
	$(this).clearQueue();
	$(this).animate({opacity: 0} , 1000, '' , function() {});	
	return false;
}


function next()
{
 	 clearListeners();
	 
	 //$('.home_images div.next').next().animate({left: document.body.clientWidth - 75} , 500, function() {});
	 $('.home_images div.next').animate({left: 70} , 1000, 'easeInOutCubic',  function() {});
	  $('.home_images div.prev').animate({left: -700 } , 1000, function() {});
	 $('.home_images div.active').animate({left: (- $('.home_images div.active').width())} , 1000, 'easeInOutCubic' , 
																	 
																	 function() {
																						 
																		// now shift the classes around
	 $('.home_images div.prev').removeClass('prev');
	 $('.home_images div.active').removeClass('active').addClass('prev');
	 $('.home_images div.next').removeClass('next').addClass('active');
	 $('.home_images div.active').next().addClass('next');
	 
	 setListeners();
	 												
																		
																	});
	 
	
}


function handlePrevMouseOver()
{
	$(this).clearQueue();
	$(this).animate({opacity:1} , 500, '', function() {});	
}

function handlePrevMouseOut()
{
	
	$(this).clearQueue();
	$(this).animate({opacity:0} , 500, '', function() {});	
	return false;
}


function prev()
{

	 clearListeners();
	 //$('.home_images div.prev').prev().animate({left: (10 - $('.home_images div.prev').width())} , 500,'easeOutBack' , function() {});
	 $('.home_images div.prev').animate({left: 70} , 1000, 'easeInOutCubic' , function() {});
	 $('.home_images div.next').animate({left: document.body.clientWidth + 100} , 1000, 'easeInOutCubic' , function() {});
	 $('.home_images div.active').animate({left: document.body.clientWidth + 100} , 1000, 'easeInOutCubic' ,  function() {
																					
																					
																					// now shift the classes around
	 $('.home_images div.next').removeClass('next');
	 $('.home_images div.active').removeClass('active').addClass('next');
	 $('.home_images div.prev').removeClass('prev').addClass('active');
	 $('.home_images div.active').prev().addClass('prev');
	 

	 setListeners();
																					
																					
																					});
	 
}


function handlePlusRollover()
{

	$(this).parent().find('div.title').animate({opacity:1} , 500 , 'easeInOutCubic' , function(){}).clearQueue().show();

}

function handlePlusRollout()
{

	$(this).parent().find('div.title').animate({opacity:0} , 500 , 'easeInOutCubic' , function(){}).clearQueue();

}

function handlePlusClickOpen()
{
	// check to see if open or closed
	$(this).parent().find('div.caption').animate({opacity:1} , 500 , 'easeInOutCubic' , function(){}).clearQueue().show();
	$(this).attr('src' , 'images/minus.png');
}

function handlePlusClickClose()
{
	// check to see if open or closed
	$(this).parent().find('div.caption').animate({opacity:0} , 500 , 'easeInOutCubic' , function(){$(this).hide()}).clearQueue();
	$(this).attr('src' , 'images/plus.png');
}


function beginSubmit(formData, jqForm, options)
{
	
	if(!validate(document.getElementById('adminForm'))) return false;
	
	// check for IE6 or IE7 and don't use ajax to submit the form if found
	if(BrowserDetect.browser == "Explorer")
	{
	
		if(BrowserDetect.version == 6 || BrowserDetect.version == 7)
		{
			
			document.getElementById('adminForm').submit();
			return false;
		}
		
	}
	$(".submit").attr("disabled", "true");
	$("#formOutput").html('Sending Data');
	
}

function showResponse(responseText, statusText, xhr, $form)  { 
    
 	$("#formOutput").html('Your details have been saved. Thank you');
    
} 

