//javascript Common functions
//var sitePath = 'http://localhost/bocapa/ngfoto/';
var sitePath = 'http://www.ngfoto.com/';

var loadingHTML = '<br/><br/><img alt="Loading" src="images/loading.gif" align="absmiddle"/><br/>Adding to Cart.';

/* portfolio */

function rotateHome()
{	
	
	var last;
	if(Element.visible('rotatorPnl')==true)
	{
		//alert('1');
		if(Element.descendantOf('rotatorTitle','rotatorPnl'))
		{
			last = Element.readAttribute('rotatorTitle','value');
		}
		//alert(last);
		effectOut('rotatorPnl');
		ajaxRotator('rotatorPnlalt', last);
		effectIn('rotatorPnlalt');
	}
	if(Element.visible('rotatorPnlalt')==true)
	{
		//alert('2');
		if(Element.descendantOf('rotatorTitle','rotatorPnlalt'))
		{
		last = Element.readAttribute('rotatorTitle','value');
		}
		//alert(last);
		effectOut('rotatorPnlalt');
		ajaxRotator('rotatorPnl', last);
		effectIn('rotatorPnl');
	}
}

function ajaxRotator(pnl, last)
{
	//alert('ajax');
	new Ajax.Updater(pnl, sitePath+'lib/rotator.php?lastid='+last, {method: 'get'});
}

//layer functions

function ShowHideLayer(id, dothis) {
	
	var L = layer(id);
	if(L.exists())
	{		
		if(dothis == "hide")
		{		
			L.setVisibility(false);
		}else
		{
			L.setVisibility(true);
		}	   
	}
}
function showpopup(id)
{	
	setOpacity("PopupOverlay");
	ShowHideLayer("PopupOverlay",'show');
	ShowHideLayer(id,'show');
}

function closePopup(name, inner)
{
	ShowHideLayer('PopupOverlay','hide');
	ShowHideLayer(name,'hide');
}

function setOpacity(name)
{
	if (navigator.appVersion.indexOf("MSIE")!=-1)
	{
		Element.setFilter(name,'alpha(opacity=80)');
	}
	else
	{
		Element.setOpacity(name,'0.8');
	}
}

/* ajax paging for categories */

var queue = Effect.Queues.get('pageScope');
var queue2 = Effect.Queues.get('pagingScope');

function effectIn(pnl)
{
	new Effect.Appear(pnl, {queue:{position:'end',scope:'pageScope',limit:2}});
}

function effectOut(pnl)
{
	new Effect.Fade(pnl, {queue:{position:'end',scope:'pageScope',limit:2}});
}

function PagingeffectIn(pnl)
{
	new Effect.Appear(pnl, {queue:{position:'end',scope:'pagingScope',limit:2}});
}

function PagingeffectOut(pnl)
{
	new Effect.Fade(pnl, {queue:{position:'end',scope:'pagingScope',limit:2}});
}

function moveCatPage(page, cat)
{
	//alert('the page is '+page+' and the cat is '+cat);
	if(Element.visible('catpage1')==true)
	{
		effectOut('catpage1');
		ajaxCatPage('catpage2', page, cat);
		effectIn('catpage2');
	}
	if(Element.visible('catpage2')==true)
	{
		effectOut('catpage2');
		ajaxCatPage('catpage1', page, cat);
		effectIn('catpage1');
	}
	if(Element.visible('catpaging1')==true)
	{
		//PagingeffectOut('catpaging1');
		Element.hide('catpaging1');
		ajaxCatPaging('catpaging2', page, cat);
		Element.show('catpaging2');
		//PagingeffectIn('catpaging2');
	}
	if(Element.visible('catpaging2')==true)
	{
		//PagingeffectOut('catpaging2');
		Element.hide('catpaging2');
		ajaxCatPaging('catpaging1', page, cat);
		Element.show('catpaging1');
		//PagingeffectIn('catpaging1');
	}	
}

function ajaxCatPage(pnl,page, cat)
{	
	new Ajax.Updater(pnl, sitePath+'lib/catlist.php?page='+page+'&catid='+cat, {method: 'get'});
}
function ajaxCatPaging(pnl,page, cat)
{
	new Ajax.Updater(pnl, sitePath+'lib/catpaging.php?page='+page+'&catid='+cat, {method: 'get'});
}

/* ajax paging for gallery */

function checkKey(page, cat, total)
    {
        var keycode;
		if (window.event) keycode = window.event.keyCode;		
		else return true;
		if (keycode == 13)
		{	
			movePageCheck(page, cat, total);
        }
    }

function movePageCheck(page, cat, total)
{
	var checkPage = RegExp("([0-9]{1,3})"); // valid page value check
	sPage = page.match(checkPage);
	if(sPage != null)
	{
		if(sPage[0] <= total)
		{
			movePage(sPage[0], cat);
		}
	}
}

function movePage(page, cat)
{
	//alert('the page is '+page+' and the cat is '+cat);
	if(Element.visible('Gallerypage1')==true)
	{
		effectOut('Gallerypage1');
		ajaxGalleryPage('Gallerypage2', page, cat);
		effectIn('Gallerypage2');
	}
	if(Element.visible('Gallerypage2')==true)
	{
		effectOut('Gallerypage2');
		ajaxGalleryPage('Gallerypage1', page, cat);
		effectIn('Gallerypage1');
	}
	if(Element.visible('imagepaging1')==true)
	{
		//PagingeffectOut('imagepaging1');
		Element.hide('imagepaging1');
		ajaxGalleryPaging('imagepaging2', page, cat);
		Element.show('imagepaging2');
		//PagingeffectIn('imagepaging2');
	}
	if(Element.visible('imagepaging2')==true)
	{
		//PagingeffectOut('imagepaging2');
		Element.hide('imagepaging2');
		ajaxGalleryPaging('imagepaging1', page, cat);
		Element.show('imagepaging1');
		//PagingeffectIn('imagepaging1');
	}
	ajaxGalleryPaging2('jmpPnl', page, cat);
}


function ajaxGalleryPage(pnl,page, cat)
{	
	new Ajax.Updater(pnl, sitePath+'lib/imagegallery.php?page='+page+'&catid='+cat, {method: 'get'});
}
function ajaxGalleryPaging(pnl,page, cat)
{
	new Ajax.Updater(pnl, sitePath+'lib/imagepaging.php?page='+page+'&catid='+cat, {method: 'get'});
}
function ajaxGalleryPaging2(pnl,page, cat)
{
	new Ajax.Updater(pnl, sitePath+'lib/jmppaging.php?page='+page+'&catid='+cat, {method: 'get'});
}

/* Cart Functions   */

function AddtoCart(image, cat)
{
	Element.update('CartActionPnl', loadingHTML);
	effectIn('CartActionPnl');
	new Ajax.Updater('CartActionPnl', sitePath+'lib/addtocart.php?image='+image+'&catid='+cat, {method: 'get'});
	effectOut('CartActionPnl');	
	Element.update('CartActionPnl', loadingHTML);
	//updateMiniCart();
}

new Ajax.PeriodicalUpdater('miniCartPnl', sitePath+'lib/minicart.php',
  {
    method: 'get',
    frequency: 1
  });

function updateMiniCart()
{
	new Ajax.Updater('miniCartPnl', sitePath+'lib/minicart.php', {method: 'get'});
}

function f_clientWidth() {
	return f_filterResults (
		window.innerWidth ? window.innerWidth : 0,
		document.documentElement ? document.documentElement.clientWidth : 0,
		document.body ? document.body.clientWidth : 0
	);
}
function f_clientHeight() {
	return f_filterResults (
		window.innerHeight ? window.innerHeight : 0,
		document.documentElement ? document.documentElement.clientHeight : 0,
		document.body ? document.body.clientHeight : 0
	);
}
function f_scrollLeft() {
	return f_filterResults (
		window.pageXOffset ? window.pageXOffset : 0,
		document.documentElement ? document.documentElement.scrollLeft : 0,
		document.body ? document.body.scrollLeft : 0
	);
}
function f_scrollTop() {
	return f_filterResults (
		window.pageYOffset ? window.pageYOffset : 0,
		document.documentElement ? document.documentElement.scrollTop : 0,
		document.body ? document.body.scrollTop : 0
	);
}

function f_filterResults(n_win, n_docel, n_body) {
	var n_result = n_win ? n_win : 0;
	if (n_docel && (!n_result || (n_result > n_docel)))
		n_result = n_docel;
	return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}


function loadEdit(catid)
{
	overlay=document.getElementById('PopupOverlay');
	page = document.body
	if(overlay)
	{
		width = f_clientWidth();
		scrollh = f_scrollTop();		
		height = page.offsetHeight;	
		
		Element.setTop('EditCat', scrollh+150);
		Element.setLeft('EditCat', width/2- 300);
		Element.setHeight('PopupOverlay', height + 150);
	}
	new Ajax.Updater('NodeInner', sitePath+'lib/getcat.php?catid='+catid, {method: 'get'});
	showpopup('EditCat');
}

function reloaddll(catid)
{	
	new Ajax.Updater('catdiv', sitePath+'lib/catddl.php?catid='+catid, {method: 'get', insertion: Insertion.Bottom});
}

function deleteCat(catid)
{
	if(confirm('Are you sure you want to delete this category?'))
	{		   
		document.location = 'manage.php?catid='+catid+'&action=del';
	}else
	{
		return false;
	}
}

function emptyCat(catid)
{
	if(confirm('Are you sure you want to empty this category?'))
	{		   
		document.location = 'manage.php?catid='+catid+'&action=empty';
	}else
	{
		return false;
	}
}
