if ( typeof Exploreos == 'undefined' ) var Exploreos = {};
if ( typeof Exploreos.Position == 'undefined' ) Exploreos.Position = {};

function getWindowAtts() {
	var scrollY = 0;

	if ( document.documentElement && document.documentElement.scrollTop ) { 
		scrollY = document.documentElement.scrollTop; 
	} else if ( document.body && document.body.scrollTop ) { 
		scrollY = document.body.scrollTop; 
	} else if ( window.pageYOffset ) { 
		scrollY = window.pageYOffset; 
	} else if ( window.scrollY ) { 
		scrollY = window.scrollY; 
	}
	if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		var winw = window.innerWidth;
		var winh = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		var winw = document.documentElement.clientWidth;
		var winh = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		var winw = document.body.clientWidth;
		var winh = document.body.clientHeight;
	}
	var test1 = document.body.scrollHeight;
	var test2 = document.body.offsetHeight;
	if (test1 > test2) {
		// all but Explorer Mac
		var bodyh = document.body.scrollHeight;
	} else {
		// Explorer Mac;
		//would also work in Explorer 6 Strict, Mozilla and Safari
		var bodyh = document.body.offsetHeight;
	}	
	return  { winWidth:winw, winHeight:winh, bodyHeight:bodyh, scrollPos:scrollY };
}

function layoutheight() {
	if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		var winh = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		var winh = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		var winh = document.body.clientHeight;
	}
	
	var test1 = document.body.scrollHeight;
	var test2 = document.body.offsetHeight;
	if (test1 > test2) {
		// all but Explorer Mac
		var bodyh = document.body.scrollHeight;
	} else {
		// Explorer Mac;
		//would also work in Explorer 6 Strict, Mozilla and Safari
		var bodyh = document.body.offsetHeight;
	}
	// reduce bodyh by 4 for IE7 Vista
	bodyh = bodyh - 4;
	document.getElementById('copyright').style.position = '';
	if (bodyh < winh || bodyh == winh) {
			document.getElementById('copyright').style.position = 'absolute';
			document.getElementById('copyright').style.bottom = '0';
			
	}
	document.getElementById('copyright').style.display = 'block';
	document.getElementById('copyright').style.visibility = 'visible';
}

// Toggle Visibility
function togglevisibility(element) {
	var el = document.getElementById(element);
	el.style.display = (el.style.display == 'none') ? 'block' : 'none';
	el.style.visibility = (el.style.visibility == 'hidden') ? 'visible' : 'hidden';
}

// Show search filters
function searchfilters(element) {
	var el = document.getElementById(element);
	if (el.style.display == 'none') { 
		Effect.BlindDown(el); 
	} else {
		Effect.BlindUp(el);
	}
}	

// Show Order Details in Admin
function adminorderdetails(cartid,status){
	$('detailsiframe').src= 'order_details.php?cart_id=' + cartid;
	
	// Handle
	$('detailshandle').removeClassName();
	$('detailshandle').className = status;
	$('detailshandle').innerHTML = 'Cart ID: ' + cartid;
	
	//size div and iframe
	sizediv('detailspopup','700','-100');
	var detailsiframe =  parseFloat($('detailspopup').getStyle('height')) - 24;
	$('detailsiframe').style.height = detailsiframe+'px';
	
	// Position popup and make visible
	centerdiv('detailspopup');
	new Effect.BlindDown('detailspopup', {duration:.5});
	var iframeoverflow = "$('detailsiframe').style.overflow = 'auto'";
	setTimeout(iframeoverflow, 500); 

	// Make draggable by a handle
	new Draggable('detailspopup', {handle:'detailshandle',starteffect:false,endeffect:false} ); 
}


// Show Order Details Popup
function showpopup(element,iframe,src){
	var type = element.split('_');
	
	// Set sizes for scrolling div/iframe so scrollbar isn't visible on effect
	if (type[0] == 'cvv2') {
		sizediv(element,'400','321');
	} else if (type[0] == 'payerauth') {
		sizediv(element,'450','311');
	} else if (type[0] == 'termsofuse') {
		sizediv(element,'700','-100');
	} 
	
	var detailscontentheight =  parseFloat($(element).getStyle('height')) - 68;
	var detailsmaincontentheight = detailscontentheight;
	$(element+'detailscontent').setStyle({  
			height: detailscontentheight+'px'
	});
	
	document.getElementById(element+'detailsmaincontent').style.height = detailsmaincontentheight+'px';
	
	// If a popup is already open, close it
	var openpopup = window.openpopup;
	if (openpopup != undefined && openpopup != element) { closepopup(openpopup); }

	// Position popup and make visible
	centerdiv(element);
	var el = $(element);
	new Effect.BlindDown(el);
	
	// Make content div visible
	if (!iframe) { 
		var elcontent = "document.getElementById('" + element + "detailsmaincontent').style.display = 'block'";
		setTimeout(elcontent, 300); 
	}
	
	// var browserName = navigator.userAgent.toLowerCase(); 
	// if (browserName.indexOf("safari") == -1) { 
	// }

	// Make draggable by a handle
	new Draggable(element, {handle:element+'thehandle',starteffect:false,endeffect:false} ); 

	// Set global variable for open popup
	window.openpopup = element;
}

function closepopup(element){
	var type = element.split('_');
	if (type[0] == 'aod') { 
		
	} else {
		var elementDimensions = Element.getDimensions(element);
		var detailscontentheight = elementDimensions['height'] - 68;
		document.getElementById(element+'detailscontent').style.height = detailscontentheight+'px';
		var elcontent = "document.getElementById('" + element + "detailsmaincontent').style.display = 'none'";
		setTimeout(elcontent, 100);
	}
	
	new Effect.BlindUp(element, {duration:.5});
}

// Message Popup
function messagepopup( type, element ) {
	centerdiv( element );
	if ( type == 'session' || type == 'error' ) {
		$( element ).style.top = '25px';
	}
	new Effect.Appear( element, { 
		afterFinish: function () {
			new Draggable( element, {
				handle: 'handle',
				starteffect: false,
				endeffect: false
				} 
			);
			if (type == 'session') {
				setTimeout("Effect.Fade('"+ element + "')", 2500);
				}
			}
		}
	);
}

function delaydisplay(element) {
	centerdiv( element );           
	new Effect.Appear( element );
	new Draggable( element );
	window.onload = function() {
		new Effect.Fade( element );
	} 
}

function resizedetailsiframe(element) {
	// Must launched on the body onload event handler for IE
	// Use document.documentElement if you are in Compat mode
	// Set to auto for 
	// iframe.style.height = 'auto';
	
	iframe = parent.$( window.name );
	parent.$( element+'details' ).show();
	
	iframeHeight = document.body.scrollHeight;
	new Effect.BlindDown( iframe,{ 
		restoreAfterFinish: false, 
		scaleMode: { originalHeight: document.body.scrollHeight } 
		}
	);
	parent.$( element+'link' ).innerHTML = 'Hide&nbsp;Details';
}


function resizepopupiframe() {
	
	var iframe = parent.$( window.name );
	var scrollHeight = document.body.scrollHeight || 150;

    if ( scrollHeight > 0 ) {
	    iframe.style.height = scrollHeight + 'px'; 
    }
}

function togglerow(element,src,rnumber,rdate) {
	var summaryrow = $(element + 'summary');
	var detailsiframe = $(element + 'detailsiframe');
	var detailsrow = $(element + 'details');
	
	var openpopup = window.openpopup;
	if (openpopup != undefined && openpopup != element) { new Effect.Fade(openpopup); }
	
	if (detailsrow.style.display == 'none') {
		var cells = summaryrow.getElementsByTagName('td'); 
		for (i=0; i<cells.length; i++) { $(cells[i]).toggleClassName('detailsrow');}
		$(element + 'number').className = 'recordicon';
		$(element + 'date').className = 'recorddetails';
		$(element + 'number').innerHTML = '<img src="images/detailslogo.gif" />';
		$(element + 'date').innerHTML = 'Details for';
		$(element + 'link').innerHTML = 'Please&nbsp;Wait&nbsp;';
		detailsiframe.src= src; 
		detailsiframe.style.display = '';
		
		var browserName = navigator.userAgent.toLowerCase(); 
		if (browserName.indexOf("safari") != -1) { 
			detailsiframe.style.height = '-10px';
			detailsrow.style.display = '';
		}
	} else {
		new Effect.BlindUp(detailsiframe, {duration:.9});
		new Effect.BlindUp(detailsrow, {duration:.9});
		funcsummaryrow = "summaryrow('"+element+"','"+rnumber+"','"+rdate+"')";
		setTimeout(funcsummaryrow, 1100);
	 }
}
function summaryrow(element,rnumber,rdate) {
	var cells = $(element + 'summary').getElementsByTagName('td'); 
	for (i=0; i<cells.length; i++) { $(cells[i]).removeClassName('detailsrow'); }
	$(element + 'number').className = 'recordnumber';
	$(element + 'date').className = 'recorddate';
	$(element + 'number').innerHTML = rnumber;
	$(element + 'date').innerHTML = rdate;
	$(element + 'link').innerHTML = 'View&nbsp;Details';
		
}
function showimage(element,src) {
	var openpopup = window.openpopup;
	if (openpopup != undefined && openpopup != element) { new Effect.Fade(openpopup); }
	
	$(element+'iframe').src= src; 
	centerdiv(element);
	var el = document.getElementById(element);
	new Effect.Appear(el, {duration:.7});
	new Draggable(element, {starteffect:false,endeffect:false} ); 
	window.openpopup = element;
}

function sizediv(element,width,height) {
	var scrollY = 0;

	if ( document.documentElement && document.documentElement.scrollTop ) { 
		scrollY = document.documentElement.scrollTop; 
	} else if ( document.body && document.body.scrollTop ) { 
		scrollY = document.body.scrollTop; 
	} else if ( window.pageYOffset ) { 
		scrollY = window.pageYOffset; 
	} else if ( window.scrollY ) { 
		scrollY = window.scrollY; 
	}
	if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		var winw = window.innerWidth;
		var winh = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		var winw = document.documentElement.clientWidth;
		var winh = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		var winw = document.body.clientWidth;
		var winh = document.body.clientHeight;
	}
	
	var operator = width.substring(0,1);
	var operand = width.substring(1,width.length); 
	if ( operator  == '-') { var calcwidth = parseFloat(winw) - parseFloat(operand); }
	else if ( operator  == '+') { var calcwidth = parseFloat(winw) + parseFloat(operand); }
	else if ( operator  == '/') { var calcwidth = parseFloat(winw) / parseFloat(operand); }
	else if ( operator  == '.') { var calcwidth = parseFloat(winw) * parseFloat(width); }
	else { var calcwidth = width; }
	
	var operator = height.substring(0,1);
	var operand = height.substring(1,height.length); 
	if ( operator  == '-') { var calcheight = parseFloat(winh) - parseFloat(operand); }
	else if ( operator  == '+') { var calcheight = parseFloat(winh) + parseFloat(operand); }
	else if ( operator  == '/') { var calcheight = parseFloat(winh) / parseFloat(operand); }
	else if ( operator  == '.') { var calcheight = parseFloat(winh) * parseFloat(height); }
	else { var calcheight = height; }
	
	document.getElementById(element).style.width = calcwidth+'px';
	document.getElementById(element).style.height = calcheight+'px';
	
	// alert(calcwidth + 'x' + calcheight);
	
}

function centerdiv(element) {
	var scrollY = 0;

	if ( document.documentElement && document.documentElement.scrollTop ) { 
		scrollY = document.documentElement.scrollTop; 
	} else if ( document.body && document.body.scrollTop ) { 
		scrollY = document.body.scrollTop; 
	} else if ( window.pageYOffset ) { 
		scrollY = window.pageYOffset; 
	} else if ( window.scrollY ) { 
		scrollY = window.scrollY; 
	}
	if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		var winw = window.innerWidth;
		var winh = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		var winw = document.documentElement.clientWidth;
		var winh = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		var winw = document.body.clientWidth;
		var winh = document.body.clientHeight;
	}
	
	var elementDimensions = Element.getDimensions(element);
	var width = elementDimensions['width'];
	var height = elementDimensions['height'];
	
	var elleft = (winw-width)/2;
	var eltop = (winh-height)/2 + scrollY;

	elleft = ( elleft < 0 ) ? 0 : elleft;
	eltop = ( eltop < 0 ) ? 0 : eltop;
	
	document.getElementById(element).style.position = 'absolute';
	document.getElementById(element).style.left = elleft+'px';
	document.getElementById(element).style.top = eltop+'px';
}
// Accordion
function accordion(element){
	var el = document.getElementById(element);
	// document.getElementById('copyright').style.display = 'none';
	// document.getElementById('copyright').style.visibility = 'hidden';
	if (el.style.display == 'none') {
		new Effect.BlindDown(el, {duration:.1});
		// setTimeout("layoutheight()", 100);
	} else {
		new Effect.BlindUp(el, {duration:.1});
		// setTimeout("layoutheight()", 100);
	}
}
// Drop Down Navigation
function dropdownnav() {
	var url=document.dropdown.nav.value;
	window.open(url,'_self');
};

// Make button submit on return
function submitform(formname) {
	document.forms[formname].submit();
}

function addInputSubmitEvent(form, input) {
    input.onkeydown = function(e) {
	    e = e || window.event;
	    if (e.keyCode == 13) {
		 // alert(form);
	        document.forms[form].submit();
	        return false;
        }
        return true;
    };
}

/*window.onload = function() {
   var forms = document.getElementsByTagName('form');

   for (var i=0; i < forms.length; i++) {
	var inputs = forms[i].getElementsByTagName('input');

	for (var j=0; j < inputs.length; j++) {
		addInputSubmitEvent(forms[i].id, inputs[j]); 
	}
    
   	var selects = forms[i].getElementsByTagName('select');
	
	for (var k=0; k < selects.length; k++) {
		addInputSubmitEvent(forms[i].id, selects[k]); 
	}
	alert(forms[i].id);
   }
}*/

function moreinfoToggle(obj,divname) {
	var el = document.getElementById(divname);
	var objPosition = findPos(obj);
	var objDimensions = Element.getDimensions(obj);
	var elDimensions = Element.getDimensions(el);
	el.style.top = objPosition[1] + 'px';
	el.style.left = ((objPosition[0] + objDimensions.width) - elDimensions.width) + 'px';
	el.style.width = elDimensions.width + 'px';
	togglevisibility(divname);
	
};
	
function findPos(element)
{	obj = element;
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	  
	return [curleft,curtop];
};


function sendButtonSelection(name,button,form) {
	myinput = document.createElement('input');
	myinput.setAttribute('type','hidden');
	myinput.setAttribute('name',name);
	myinput.setAttribute('value',button);
	document.forms[form].appendChild(myinput);
	document.forms[form].submit();
}

function determineDateDisplay() {
	var date_option_value = document.search_form.date_option.value;
	var date_range_div = document.getElementById('date_range');
	if ( date_option_value == 'daily' || date_option_value == 'weekly' || date_option_value == 'day_of_week' ) {
		date_range_div.style.display = 'block';
		date_range_div.style.visibility = 'visible';
	} else {
		date_range_div.style.display = 'none';
		date_range_div.style.visibility = 'hidden';	
	}
}

var submenu = {
		show : function(obj,element) {
			submenu.hide(obj,element);
			$(obj).className = 'selected';
			if ($('submenu').style.display != 'block') {
				$('submenu').style.display = 'block';
				new Effect.SlideDown(element, {duration:.2});
			} else { $(element).style.display = 'block'; }
		},
		hide : function(obj,element) {
			var submenulists = $('submenu').getElementsByTagName('ul');  
			for (i=0; i<submenulists.length; i++) { 
				if ($(submenulists[i]).style.display != 'none' && submenulists[i].id != element) { $(submenulists[i]).style.display = 'none'; }
			}
			if (!element && $('submenu').style.display != 'none') { new Effect.SlideUp('submenu', {duration:.1}); }
			var menulinks = $('menu').getElementsByTagName('a');  
			for (i=0; i<menulinks.length; i++) { 
				if (menulinks[i].id != obj.id) { $(menulinks[i]).removeClassName('selected'); }
			}
		}
	};
Exploreos.Position.GetWindowSize = function(win) {
    var width, height, base;

    win     = win ? win : window;
    base    = win.document;

    width  = win.innerWidth
               || (base.documentElement.clientWidth || base.body.clientWidth);
    height = win.innerHeight
               || (base.documentElement.clientHeight || base.body.clientHeight);

    return { width: width, height: height };
};

Exploreos.Position.Center = function(object, parent) {
    var objectWidth, objectHeight, parentWidth, parentHeight;
    var cumulativeOffset = [0, 0];
    var coords           = { top: 0, left: 0 };

    Position.prepare();

	var dim = Element.getDimensions(object);
	objectWidth  = dim.width;
	objectHeight = dim.height;


    if ( !parent ) {
	    var windowSize  = Exploreos.Position.GetWindowSize();
	    parentWidth     = windowSize.width;
	    parentHeight    = windowSize.height;
	}
    else {
        var parentDim   = Element.getDimensions(parent);
	    parentWidth     = parentDim.width;
	    parentHeight    = parentDim.height;

        cumulativeOffset = Position.cumulativeOffset($(parent));
	}

    //object.style.top  = ((parentHeight - objectHeight) / 2) -  Position.deltaY + "px";
	//object.style.left = ((parentWidth  - objectWidth ) / 2) -  Position.deltaX + "px";
    coords = {
        top:    ((parentHeight - objectHeight) / 2) +  cumulativeOffset[1],
        left:   ((parentWidth  - objectWidth ) / 2) +  cumulativeOffset[0]
    };

    return coords;
};

Exploreos.Position.centerOnElement = function(child, parent, messageType) {
    Position.prepare();

    var centerCoordsWindow = Exploreos.Position.Center(child);
    var centerCoordsParent = Exploreos.Position.Center(child, parent);
    /**
     * First, determine the offsets of the parent in order to
     * calculate the child's new position (relative to the parent).
     *
     * Position.realOffset: cumulative scroll offsets from within nested cntners
     * Position.cumulativeOffset:   offsets from the top left corner of document
     */
    var realOffset       = Position.realOffset($(parent));
    var cumulativeOffset = Position.cumulativeOffset($(parent));

    /**
     * Get the widths of both the parent and child elements
     * to determine the horizontal offset for the child
     */
    var parentWidth = Element.getWidth($(parent));
    var childWidth  = Element.getWidth($(child));

    var parentDim   = Element.getDimensions($(parent));
    var childDim    = Element.getDimensions($(child));

    var windowSize  = Exploreos.Position.GetWindowSize();

    /* Vertical play; maybe overridden at runtime */
    var viewportOffset = 75;

    /* Calculate the offsets of the new position for the child */
    var deltaWidth  = ((parentDim.width  - childDim.width ) / 2);
    var deltaHeight = ((parentDim.height - childDim.height) / 2);

    var offsetLeft  = deltaWidth  + cumulativeOffset[0];
    var offsetTop   = deltaHeight + cumulativeOffset[1];

    offsetLeft  = (offsetLeft < realOffset[0]) ? (realOffset[0] + 10) : offsetLeft;

    if ( messageType == 'warn' || messageType == 'mess' ) {
        offsetTop   = (cumulativeOffset[1] + viewportOffset);
	offsetTop   = (offsetTop  < realOffset[1]) ? (realOffset[1] + viewportOffset) : offsetTop;
    }
    else if ( messageType == 'info' ) {
	offsetTop   = ((windowSize.height - childDim.height) /2 ) + realOffset[1];
        offsetTop   = (offsetTop  < realOffset[1]) ? (realOffset[1] + 10) : offsetTop;
    } 
    
    /*
    console.group('Exploreos.Position.centerOnElement()');
    console.debug('cumulativeOffset         = %d (%s)', cumulativeOffset.inspect(), parent.inspect());
    console.debug('cumulativeOffset         = %d (%s)', Position.cumulativeOffset($(child)).inspect(), child.inspect());
    console.debug('realOffset               = %s (%s)', realOffset.inspect(), parent.inspect());
    console.debug('realOffset               = %s (%s)', Position.realOffset($(child)).inspect(), child.inspect());
    console.debug('Dimensions               = %s (%s)', $H(parentDim).inspect(), parent.inspect());
    console.debug('Dimensions               = %s (%s)', $H(childDim).inspect(), child.inspect());
    console.debug('==================================================');
    console.debug('Window Size              = %s', $H(windowSize).inspect());
    console.debug('Center Position (window) = %s', $H(centerCoordsWindow).inspect());
    console.debug('Center Position (parent) = %s', $H(centerCoordsParent).inspect());
    console.debug('offsets                  = [ top: %s, left: %s ]', offsetTop, offsetLeft);
    console.groupEnd();
    */

    /* Position the child at the calculated offsets */
    $(child).setStyle({
        position:   'absolute',
        top:        offsetTop + 'px',
        left:       offsetLeft + 'px'
    });

    /**
     * Apply transitions to render the child into view and allow for
     * drag-and-drop control.
    
    new Effect.Appear(child);
	new Draggable(child, {
        handle:         child + 'handle',
        starteffect:    false,
        endeffect:      false
    });
    if ( messageType == 'mess' ) {
	    setTimeout("Effect.Fade($('" + child + "'))", 2500);
    } */
};
