//=======================================================================================================
//-------------------------------------------------------------------------------------------------------
//	Bind actions
//-------------------------------------------------------------------------------------------------------
//=======================================================================================================
$(document).ready(function()
{	
	jQuery('#header-search input[name="findtext"]').liveSearch({url: '/panes/search.php?findtext='});		
	
	$('ul.sub > li > a.collection').bind(	'mouseover', showCollectionsPopup	),
	$('ul.sub > li > a.collection').bind(	'mouseout',  hideCollectionsPopup	),
	$('a.productdetails').bind(				'click',     showProductPopup	),
	$('#popupdiv').bind(					'mouseover', popupOver	),
	$('#popupdiv').bind(					'mouseout',  popupOut	),
	
	$('.filter > a.collection').bind(		'mouseover', showCollectionsPopup	),
	$('.filter > a.collection').bind(		'mouseout',  hideCollectionsPopup	)
	$('a.collection').bind(	'mouseover',	showCollectionsPopup	),
	$('a.collection').bind(	'mouseout',		hideCollectionsPopup	),

	$('.filter > a.collection1').bind(		'mouseover', showCollectionsPopup1	),
	$('.filter > a.collection1').bind(		'mouseout',  hideCollectionsPopup	)
	$('a.collection1').bind(	'mouseover',	showCollectionsPopup1	),
	$('a.collection1').bind(	'mouseout',		hideCollectionsPopup	)

	$('.filter > a.collection2').bind(		'mouseover', showCollectionsPopup2	),
	$('.filter > a.collection2').bind(		'mouseout',  hideCollectionsPopup	)
	$('a.collection2').bind(	'mouseover',	showCollectionsPopup2	),
	$('a.collection2').bind(	'mouseout',		hideCollectionsPopup	)

});

//=======================================================================================================
//-------------------------------------------------------------------------------------------------------
//	Custom Javascript functions
//-------------------------------------------------------------------------------------------------------
//=======================================================================================================

function AddToCart()
{
	var options = { 
        target:        '#productpopupdiv'
    }; 
	
	$('#productpopupdiv #updatecart').ajaxSubmit(options); 
}

function ValidateEmailForm(formData, jqForm, options) 
{ 
	if ( validateemailafriend() )
	{
		return true;
	}

	else
	{
		return false;
	}
} 
	
function newSize(width,height) {

    if(document.all && !document.getElementById) {

      document.all['brahminFlash'].style.pixelWidth = width;

      document.all['brahminFlash'].style.pixelHeight = height;

    }else{

        document.getElementById('brahminFlash').style.width = width;

        document.getElementById('brahminFlash').style.height = height;

    }

}

var popupactive = false;

function setFooter()
{
	top1 = document.getElementById('nav-main-outer').offsetHeight + 30;
	top2 = document.getElementById('main-page-area').offsetHeight + 140;
	
	if (top1 > top2)
	{
		document.getElementById('footer').style.marginTop	= (top1-top2) + 'px';
	}

	document.getElementById('footer').style.display		= 'block';
}

function resetPopup()
{
	if (document.getElementById('productpopupdiv-out').style.display == 'block')
	{
		$("#productpopupdiv-out").center(true);
	}
}

function showCollectionsPopup()
{
	var collectionId = $(this).attr("itemid");

	if (!collectionId)
	{
		return;
	}

	$('#popupdiv').html(""); 
	$('#popupdiv').load("/panes/collection.php?id="+collectionId); 
	
	var offset = $(this).offset();
	var width  = $(this).width();

	$("#popupdiv").css('top',		offset.top + 'px');
	$("#popupdiv").css('left',		140 + 'px');
	$("#popupdiv").css('width',		'327px');
	$("#popupdiv").css('height',	'151px');
	$("#popupdiv").css('display',	'block');
}

function showCollectionsPopup1()
{
	var collectionId = $(this).attr("itemid");

	if (!collectionId)
	{
		return;
	}

	$('#popupdiv').html(""); 
	$('#popupdiv').load("/panes/collection.php?id="+collectionId+"&type=1"); 
	
	var offset = $(this).offset();
	var width  = $(this).width();

	$("#popupdiv").css('top',		offset.top + 'px');
	$("#popupdiv").css('left',		140 + 'px');
	$("#popupdiv").css('width',		'327px');
	$("#popupdiv").css('height',	'151px');
	$("#popupdiv").css('display',	'block');
}

function showCollectionsPopup2()
{
	var collectionId = $(this).attr("itemid");

	if (!collectionId)
	{
		return;
	}

	$('#popupdiv').html(""); 
	$('#popupdiv').load("/panes/collection.php?id="+collectionId+"&type=2"); 
	
	var offset = $(this).offset();
	var width  = $(this).width();

	$("#popupdiv").css('top',		offset.top + 'px');
	$("#popupdiv").css('left',		140 + 'px');
	$("#popupdiv").css('width',		'327px');
	$("#popupdiv").css('height',	'151px');
	$("#popupdiv").css('display',	'block');
}

function hideCollectionsPopup()
{
	var collectionId = $(this).attr("itemid");

	if (!collectionId)
	{
		return;
	}

	if (popupactive)
	{
		return;
	}
	
	$("#popupdiv").css('display',	'none');
}

var productOpen = false;

function showProductPopup(id,ignoreSecondary)
{
	if (!id || !parseInt(id) )
	{
		var productId = $(this).attr("itemid");
	}

	else
	{
		var productId = id;
	}

	if (!productId)
	{
		return;
	}

	if (!document.getElementById( 'zoom1' ))
	{
		if ( !ignoreSecondary && productOpen && document.getElementById( 'productpopupdiv-out' ).style.display == 'block')
		{
			var currentPopupContents = $('#productpopupdiv').html();
				
			if (currentPopupContents)
			{
				$('#productpopupdiv-back').html(currentPopupContents);
				$("#productpopupdiv-back").css('width',		'600px');
				$("#productpopupdiv-back").css('height',	'500px');
				$("#productpopupdiv-back").css('display',	'block');
				$("#productpopupdiv-out").css('width',		'912px');
				$("#productpopupdiv-out").center(true);
			}
		}
	}
	
	$('#productpopupdiv').html(""); 
	$('#productpopupdiv').load("/panes/product.php?id="+productId ); 
	$("#productpopupdiv").css('width',	'600px');
	$("#productpopupdiv").css('height',	'500px');
	$("#productpopupdiv-out").center(true);
	$("#productpopupdiv-out").css('display', 'block');

	productOpen = true;
}

function swapProductPanes()
{
	primaryHTML		= $('#productpopupdiv').html();
	secondaryHTML	= $('#productpopupdiv-back').html();

	$('#productpopupdiv-back').html(primaryHTML);
	$('#productpopupdiv').html(secondaryHTML);
}

function hideProductPopup()
{
	if (document.getElementById( 'productpopupdiv-back' ).style.display == 'block')
	{
		var currentPopupContents = $('#productpopupdiv-back').html();
		$('#productpopupdiv').html(currentPopupContents);
		$("#productpopupdiv-back").css('display', 'none');
		$("#productpopupdiv-out").css('width',		'610px');
		$("#productpopupdiv-out").center(true);
	}

	else
	{
		productOpen = false;
			
		$("#productpopupdiv-out").css('width',		'610px');
		$("#productpopupdiv-back").css('display', 'none');
		$("#productpopupdiv-out").css('display', 'none');
	}
}

function showProductPane(url)
{
	if (!url)
	{
		return;
	}

	$("#productpopupdiv-out").css('width',		'610px');
	$("#productpopupdiv-back").css('display', 'none');
	$('#productpopupdiv').html('<CENTER><IMG style="margin-top:230px;" SRC="/images/spinner.gif" BORDER="0" ALT=""></CENTER>'); 
	$('#productpopupdiv').load(url, function() { setFooter() }); 
	$("#productpopupdiv").css('width',	'600px');
	$("#productpopupdiv").css('height',	'500px');
	$("#productpopupdiv-out").center(true);
	$("#productpopupdiv-out").fadeIn(200);
}

function popupOver()
{
	$("#popupdiv").css('display',	'block');
	popupactive = true;
}

function popupOut()
{
	popupactive = false
	$("#popupdiv").css('display',	'none');
}

function showPane(url)
{
	if (!url)
	{
		return;
	}
	
	$("#productpopupdiv-out").css('width',		'610px');
	$("#productpopupdiv-back").css('display', 'none');
	$('#productpopupdiv').html(""); 
	$('#productpopupdiv').load(url); 
	$("#productpopupdiv").css('width',	'600px');
	$("#productpopupdiv").css('height',	'500px');
	$("#productpopupdiv-out").center(true);
	$("#productpopupdiv-out").fadeIn(200);
}

function showDiv( id )
{	
	if( document.getElementById(id) )
	{
		document.getElementById(id).style.display='block';
		setFooter();
	}
}

function loadHTML( div, url )
{
	$(div).load(url, function() { setFooter() });
}

//-------------------------------------------------------------------------------------------------------
//	Image swap
//-------------------------------------------------------------------------------------------------------
function swapImg(id,img)
{
	if( document.getElementById)
	{
		if( document.getElementById(id))
		{	
			obj	= document.getElementById(id);
			obj.setAttribute( 'src', img );
		}
	}
}