/**
 * @author Anthony
 */

var sPath 			= window.location.pathname;
var sPage 			= sPath.substring(sPath.lastIndexOf('/') + 1);
var page			= sPage.split(".")[0];
var pages			= [];

$(document).ready(function()
{
	Cufon.now();
	
	$(".showItem").click(function()
	{
		var pid = this.href.substring(this.href.lastIndexOf("/") + 1);
		showItem(pid);
		
		$("#swatch").show();
		var yPos = $('#collection_gallery_title').position().top;
		window.scrollTo(0,yPos);
		
		return false;
	});	
	
	
	$(".showVideo").click(function()
	{
		var pid = this.href.substring(this.href.lastIndexOf("/") + 1);
		showVideo(pid);
		
		$("#swatch").hide();
		var yPos = $('#collection_gallery_title').position().top;
		window.scrollTo(0,yPos); 
		
		return false;
	});	
	
	$.validator.addMethod("verifyText", 
		function(value, element)
		{
			if(checkValue(element.value))
			{
				return false;
			} else
			{
				return true;
			}
		}
	);
	
	/*///////////////////////////// SHOWS DEFAULT TEXT WITHIN FORM ELEMENTS */
	$(".defaultText").focus(function(srcc)
    {
        if ($(this).val() == $(this)[0].title && !$(this).attr('readonly'))
        {
			$(this).removeClass("defaultTextActive");
            $(this).val("");
        }
    });
	
	$(".defaultText").blur(function()
    {
        if ($(this).val() == "")
        {
			$(this).addClass("defaultTextActive");
            $(this).val($(this)[0].title);
        }
    });
    
    $(".defaultText").blur();
	
	var container = $('div.errorContainer');
	
	$("#contactForm").validate(
	{
		errorContainer: container,
		errorLabelContainer: $("ul", container),
		wrapper: 'li',
		meta: "validate"
	});
	
	$('.send').click(function()
	{
		$('#contactForm').submit();
	});
});

function showItem(id)
{
	$('#item').html('');
	
	var loadingContainer 	= document.createElement('div');
	loadingContainer.id		= "imageOverlayPreloaderContainer";
	
	var loading				= document.createElement('div');
	loading.id				= "imageOverlayPreloader";
	
	loadingContainer.appendChild(loading);
	$('#item').append(loadingContainer);
	
	$.ajax(
	{
		url: 'query.php',
		type: 'POST',
		data: 'pid=' + id + '&type=product',
		success: function(data)
		{
			$('#item').html(data);
	    	Cufon.refresh();
		}
	});
	
	return false;
}

function showVideo(id)
{
	$('#item').html('');
	
	var loading	= document.createElement('div');
	loading.id	= "imageOverlayPreloader";
	
	$('#item').append(loading);
	
	$.ajax(
	{
		url: 'query.php',
		type: 'POST',
		data: 'pid=' + id + '&type=video',
		success: function(data)
		{
			$('#item').html(data);
	    	Cufon.refresh();
		}
	});
	
	return false;
}

function imageOverlay(src)
{
	$('#overlay').remove();
	
	var viewportHeight 	= $(document).height();
	var viewportWidth 	= $(window).width();
	
	var overlay 		= document.createElement('div');
	var imageContainer	= document.createElement('div');
	var image			= document.createElement('div');
	var close			= document.createElement('div');
	var loading			= document.createElement('div');
	var img				= document.createElement('img');
	img.src				= src;
	overlay.id			= "overlay";
	imageContainer.id	= "imageOverlay";
	image.id			= "imageOverlayImage";
	loading.id			= "imageOverlayPreloader";
	close.id			= "imageOverlayClose";
	
	document.body.appendChild(overlay);
	document.body.appendChild(imageContainer);
	imageContainer.appendChild(image);
	imageContainer.appendChild(close);
	image.appendChild(loading);
	image.appendChild(img);
	
	$(overlay).css('width', viewportWidth);
	$(overlay).css('height', viewportHeight);
	$(overlay).css('opacity', '.75');
	$(img).hide();
	
	var yPos = (($(window).height() - $(imageContainer).height()) * .5) + $(window).scrollTop();
	var xPos = ($(window).width() - $(imageContainer).width()) * .5;
	
	$(imageContainer).css('top', yPos);
	$(imageContainer).css('left', xPos);
	
	$(img).load(function()
	{
		$(loading).remove();
		
		xPos = ($(window).width() - $(img).width()) * .5;
		yPos = (($(window).height() - $(img).height()) * .5) + $(window).scrollTop();
		
		$(imageContainer).animate({width:$(img).width(), height:$(img).height(), top:yPos, left:xPos}, 700);
		$(img).show();
	});	
	
	$(close).click(function()
	{
		$('#overlay').remove();
		$(imageContainer).remove();
	});
}

var currentGallery	= null;
var position 		= 0;
var currentPosition	= 0;
var currentPic		= '';

function showImage(parent, direction)
{
	var photos 	= $("#" + parent + " .photos").children('.photo');
	var total	= photos.length - 1;
	var $active = $("#" + parent + ' .photos div.active');
	
	if (currentGallery != parent)
	{
		position		= $('#position_' + parent).val();
		currentPosition = $('#position_' + parent).val();
		currentPic 		= $(photos[position]);
	}
	
	if (direction == "right" && position < total) position++;	
	if (direction == "left" && position > 0) position--;
	
	var $newImg	= $(photos[position]);
	
	if (currentPic.attr('id') != $newImg.attr('id'))
	{
		$("#bullets_" + parent + currentPosition).removeClass('active');
		$("#bullets_" + parent + position).addClass('active');
		
		$newImg.css({opacity: 0.0}).addClass('active').animate({opacity: 1.0}, 700);
		$active.animate({opacity: 0.0}, 700, function(){$active.removeClass('active');});
		
		currentPic 		= $newImg;	
		currentGallery 	= parent;
		currentPosition = position;
		
		$('#position_' + parent).val(position);
	}
	
	return false;
}
/**
 * Checks a valus against a list of default values
 * @param value to be checked
 */
function checkValue(val)
{
	var isDefault 	= false;
	var list		= ['Your email address','Introduce yourself and tell us what you’re looking for.'];
	var total		= list.length;
	
	for(var x = 0; x < total; x++)
	{
		if(val == list[x])
		{
			isDefault = true;
			break;
		}
	}
	
	return isDefault;
}
$(window).scroll(function()
{
	var viewportHeight 	= $(window).height() + $(window).scrollTop();
	//$('#overlay').css('height', viewportHeight);
});

/*///////////////////////////////// HOME PAGE SLIDESHOW */
function slideSwitch(parent)
{
	if($(parent).children("img").length)
	{
	    var $active = $(parent + ' img.active');
	    
	    if ($active.length == 0) $active = $(parent + 'img:last');
	    
	    var $next = ($active.next("img").length) ? $active.next("img") : $(parent + ' img:first');
	    
	    $active.addClass('last-active');
	 
	    $next.css({opacity: 0.0})
	        .addClass('active')
	        .animate({opacity: 1.0}, 1000, function() {
	            $active.removeClass('active last-active');
	        });
	}
}
 
function startSlideShow(parent, time)
{
    setInterval( "slideSwitch('" + parent + "')", time );
}
