/* 
Contains code from Sérgio Dinis Lopes
Copyright (c) 2010 sergiolopes.name
*/

$(document).ready(function(){
	placesitemiddle();
	if($('.contentscrollable').length > 0){reloadScroller();}
});
$(window).resize(function(){
	placesitemiddle();
});

/*
 Place website always on the middle of the viewport
*/
function placesitemiddle(){
	$('body').hide(); // for IE	to avoid the website "jump" (not always results :S)

	var siteheight = $('body').height();
	var viewportHeight = $(window).height();
	
	if(viewportHeight < 400){$('body').show().css('margin-top','0');return;}
	var halfVP = (viewportHeight / 2);
	var halfSite = (siteheight / 2);
	$('body').css({marginTop:(halfVP-halfSite)+'px'});
	$('body').show(); // for IE	to avoid the website "jump" (not always results :S)
	
}

/*
 Processing...
*/
function processing(){
	if($('#processing').length == 0){
		$('#wrap').prepend('<div id="processing"></div>');
		$('#processing').hide().delay(1000).fadeIn().text(DC_Processing);
	}
}
/*
 Processed...
*/
function processed(){
	if($('#processing').is(':hidden')){$('#processing').remove();return;}
	$('#processing').fadeOut(100,function(){$('#processing').remove()});
}

function scrollableContent(scrollelem){
	
	if(scrollelem.length==0)var scrollelem = '.contentscrollable';
	
	if($(scrollelem).length > 0){
		
		var divheight = $(scrollelem).innerHeight();
		var scrollUpValue = (divheight-divheight)-divheight;
		var scrollDownValue = divheight;
		var scrollheight = $(scrollelem).attr("scrollHeight");
		var divposition = $(scrollelem).scrollTop();
		
		// Disable up button if back to start
		if(divposition==0){
			$('.scrollcontrols a.scrollup').addClass('disabled');
		}
		else $('.scrollcontrols a.scrollup').removeClass('disabled');

		// Disable down button if on bottom
		var heightposition = bottomScrollHeight(scrollelem);
		if(heightposition<1)$('.scrollcontrols a.scrolldown').addClass('disabled');
		else $('.scrollcontrols a.scrolldown').removeClass('disabled');
		
		var isactive = $('.scrollcontrols').hasClass('activated');
		if(isactive)return;
		
		// Scroll with mousewheel
		$(scrollelem).bind('mousewheel', function(event, delta) {
			if(delta > 0)var dir = scrollUpValue;
			else var dir = scrollDownValue;
			
			var value = $(this).scrollTop();
			$(this).animate({scrollTop: dir + value},500, function(){
				
				// Disable up button if back to start
				var divposition = $(this).scrollTop();
				if(divposition==0)$('.scrollcontrols a.scrollup').addClass('disabled');
				else $('.scrollcontrols a.scrollup').removeClass('disabled');

				// Disable down button if on bottom
				var heightposition = bottomScrollHeight(this);
				if(heightposition<1)$('.scrollcontrols a.scrolldown').addClass('disabled');
				else $('.scrollcontrols a.scrolldown').removeClass('disabled');
				
			});
		});
		
		// Click event for Scroll Up button
		$('.scrollcontrols a.scrollup').click(function(){
   			var value = $(scrollelem).scrollTop();
			$(scrollelem).animate({scrollTop: scrollUpValue + value},500, function(){
																							 
				// Disable up button if back to start
				var divposition = $(this).scrollTop();
				if(divposition==0)$('.scrollcontrols a.scrollup').addClass('disabled');
				else $('.scrollcontrols a.scrollup').removeClass('disabled');

				// Disable down button if on bottom
				var heightposition = bottomScrollHeight(this);
				if(heightposition<1)$('.scrollcontrols a.scrolldown').addClass('disabled');
				else $('.scrollcontrols a.scrolldown').removeClass('disabled');
				
			});
		});
		
		// Click event for Scroll Down button
		$('.scrollcontrols a.scrolldown').click(function(){
   			var value = $(scrollelem).scrollTop();
			$(scrollelem).animate({scrollTop: scrollDownValue + value},500, function(){
																							   
				// Disable up button if back to start
				var divposition = $(this).scrollTop();
				if(divposition==0)$('.scrollcontrols a.scrollup').addClass('disabled');
				else $('.scrollcontrols a.scrollup').removeClass('disabled');

				// Disable down button if on bottom
				var heightposition = bottomScrollHeight(this);
				if(heightposition<1)$('.scrollcontrols a.scrolldown').addClass('disabled');
				else $('.scrollcontrols a.scrolldown').removeClass('disabled');
				
			});
		});
		$('.scrollcontrols').addClass('activated');
	}
	return false;
}



function imgSlide(hovereffect){
	
	$('#navibuttons').hide();
	if(hovereffect){
		$('.slide').hover(
			function (){
				$('#navibuttons').show();
			},
			function (){
				$('#navibuttons').hide();
			}	
		);
	}
	
	// Hide all images
	$('.theimages img').hide();
	// Show only the first one
	var images = $('.theimages img');
	$(images[0]).show().attr({'id':'show'});
	// Hide the prev button
	var previmg = $('#show').prev();
	if($(previmg).length==0)$('a.previmg').hide();
	
	// if only one image, hide next
	if($('.theimages img').length==1)$('a.nextimg').hide();
	
	
	$('a.nextimg').click(function(){
		var nextimg = $('#show').next();
		if($(nextimg).nextAll().length==0)$('a.nextimg').hide();
		if($(nextimg).length==0)return;
		$('a.previmg').show();
		$('#show').hide().removeAttr('id');
		$(nextimg).show().attr({'id':'show'});
	});
	$('a.previmg').click(function(){
		var previmg = $('#show').prev();
		if($(previmg).prevAll().length==0)$('a.previmg').hide();
		if($(previmg).length==0)return;
		$('a.nextimg').show();
		$('#show').hide().removeAttr('id');
		$(previmg).show().attr({'id':'show'});
	});
	
}



function bottomScrollHeight(scrollelem) {
	// Get the current Div Position												   
	var divheight = $(scrollelem).innerHeight();
	var scrollheight = $(scrollelem).attr("scrollHeight");
	var scrollposition = $(scrollelem).scrollTop();
	return (scrollheight - scrollposition) - divheight;	
}

function goToBottom(scrollelem) {
	// Get the current Div Position												   
	var divheight = $(scrollelem).innerHeight();
	var scrollheight = $(scrollelem).attr("scrollHeight");
	if(divheight<scrollheight){
		$(scrollelem).scrollTop(scrollheight);
		$('.scrollcontrols a.scrolldown').addClass('disabled');
		return true;
	}
	return false;
}


function showRingInfoBox(){
	processing();
	if($('.SmallBox').length>0)$('.SmallBox').remove();
	$.post(thisurl + "/jewelry.php",{'loadringinfo':1},
		function(data){
			$('#content_wrap').prepend(data); // Append the data to HTML
			processed(); // End Processing
			
			// Hide some elements
			$('.SmallBox h2 span').hide();
			$('.SmallBox .sizes ul:last').css({'margin-right':'0'});
			$('.SmallBox .information').hide();
			// SliderElement Animation
			$('.SmallBox').animate({'width':'310px'},500,
			   function(){
					$('.SmallBox h2 span').fadeIn();
					$('.SmallBox .information').fadeIn();
			});
			
			// This is to handle the "close" button
			$('.SmallBox h2 span.close').click(function(){
				$('.SmallBox h2 span').fadeOut(100);
				$('.SmallBox .information').fadeOut(100);
				$('.SmallBox').animate({'width':'0'},200,
					function(){
					$('.SmallBox').remove();
				});
			});
	});
}


$.fn.clearForm = function() {
	return this.each(function() {
		var type = this.type, tag = this.tagName.toLowerCase();
		if (tag == 'form') return $(':input',this).clearForm();
		if (type == 'text' || type == 'password' || tag == 'textarea')
			this.value = '';
		else if (type == 'checkbox' || type == 'radio')
			this.checked = false;
		else if (tag == 'select')
			this.selectedIndex = -1;
	});
};



