function popupWindow( url, w, h )
{
	if( typeof( w ) == 'undefined' )
		w = 400;
	if( typeof( h ) == 'undefined' )
		h = 400;
  window.open(url,'popupWindow','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=yes,copyhistory=no,width='+w+',height='+h+',screenX=150,screenY=150,top=150,left=150')
}

function popupWindowPrice( url )
{
  window.open(url,'popupWindow','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,copyhistory=no,width=600,height=400,screenX=150,screenY=150,top=150,left=150')
	return false;
}

function addFavorite()
{
  if( ( navigator.appName == "Microsoft Internet Explorer" ) && ( parseInt( navigator.appVersion ) >= 4 ) )
  	window.external.AddFavorite( window.location, window.document.title );
  else if ( navigator.appName == "Netscape" )
    window.sidebar.addPanel( window.document.title, window.location, "" );
  else
    alert( "Press CTRL-D (Netscape) or CTRL-T (Opera) to bookmark" );
}

function openProductTab( t )
{
	/*
	document.getElementById('product_details_tab_personalize').className = 'product_details_tab';
	document.getElementById('product_details_tab_details').className = 'product_details_tab';
	document.getElementById('product_details_tab_shipping').className = 'product_details_tab';

	document.getElementById('product_details_panel_personalize').style.display = 'none';
	document.getElementById('product_details_panel_details').style.display = 'none';
	document.getElementById('product_details_panel_shipping').style.display = 'none';

	document.getElementById('product_details_panel_'+t).style.display = 'block';
	document.getElementById('product_details_tab_'+t).className = 'product_details_tab product_details_tab_active';
	*/

	document.getElementById('product_details_panel_'+t).style.display = 'block';

	return false;
}

function formatCurrency( n )
{
	n += 0.001;
	n = '$' + n;
	return n.substr( 0, n.length - 1 );
}

// Resize poup image
function resize() {
  var i=0;
//  if (navigator.appName == 'Netscape') i=20;
  if (window.navigator.userAgent.indexOf('MSIE 6.0') != -1 && window.navigator.userAgent.indexOf('SV1') != -1) {
      i=30; //This browser is Internet Explorer 6.x on Windows XP SP2
  } else if (window.navigator.userAgent.indexOf('MSIE 6.0') != -1) {
      i=0; //This browser is Internet Explorer 6.x
  } else if (window.navigator.userAgent.indexOf('Firefox') != -1 && window.navigator.userAgent.indexOf("Windows") != -1) {
      i=25; //This browser is Firefox on Windows
  } else if (window.navigator.userAgent.indexOf('Mozilla') != -1 && window.navigator.userAgent.indexOf("Windows") != -1) {
      i=45; //This browser is Mozilla on Windows
  } else {
      i=80; //This is all other browsers including Mozilla on Linux
  }
  if (document.documentElement && document.documentElement.clientWidth) {
//    frameWidth = document.documentElement.clientWidth;
//    frameHeight = document.documentElement.clientHeight;

	if( typeof document.images[0] == 'undefined' )
	{
		imgWidth = 500;
		imgHeight = 400;
	}
	else
	{
	  imgHeight = document.images[0].height+40-i;
	  imgWidth = document.images[0].width+20;
	}

  var height = screen.height;
  var width = screen.width;
  var leftpos = width / 2 - imgWidth / 2;
  var toppos = height / 2 - imgHeight / 2;

    frameWidth = imgWidth;
    frameHeight = imgHeight+i;

  window.moveTo(leftpos, toppos);


//  window.resizeTo(imgWidth, imgHeight);
  window.resizeTo(frameWidth,frameHeight+i);
	}
  else if (document.body) {
    window.resizeTo(document.body.clientWidth, document.body.clientHeight-i);
  }
  self.focus();
}

var G_DESIGNER_STEP = 1;
function selectProductStep(newStep)
{
	var oldStep = G_DESIGNER_STEP;
	var oldStepImg = document.getElementById( 'step_image_'+oldStep );
	var newStepImg = document.getElementById( 'step_image_'+newStep );
	var oldStepCon = document.getElementById( 'step_container_'+oldStep );
	var newStepCon = document.getElementById( 'step_container_'+newStep );

	oldStepImg.src = 'includes/templates/isidora/images/step_'+oldStep+'_off.gif';
	newStepImg.src = 'includes/templates/isidora/images/step_'+newStep+'.gif';

	oldStepCon.style.display = 'none';
	newStepCon.style.display = 'block';
	
	G_DESIGNER_STEP = newStep;

	updateProductStepHeader();
}

function resetPageNumbers()
{
	$('div.product_pattern_gallery_scroll').each( function() {
		if( this.parentNode.id != 'scroller_10' )
			return;
		var numpages = Math.ceil( $(this).children('span').not('.product_pattern_item_hidden').length / 8 );
		var oid = parseInt(this.parentNode.id.substr(9));
		document.getElementById('scroller_'+oid+'_up').src='includes/templates/isidora/images/scroll_left_off.gif';
		document.getElementById('scroller_'+oid+'_up_aux').src='includes/templates/isidora/images/scroll_left_off.gif';
		if( numpages > 1 )
		{
			document.getElementById('scroller_'+oid+'_down').src='includes/templates/isidora/images/scroll_right.gif';
			document.getElementById('scroller_'+oid+'_down_aux').src='includes/templates/isidora/images/scroll_right.gif';
		}
		else
		{
			document.getElementById('scroller_'+oid+'_down').src='includes/templates/isidora/images/scroll_right_off.gif';
			document.getElementById('scroller_'+oid+'_down_aux').src='includes/templates/isidora/images/scroll_right_off.gif';
		}
		var scrollarrows = $('div#product_pattern_gallery_scrollarrows_'+oid+' a');
		var scrollarrowsAux = $('div#product_pattern_gallery_scrollarrows_'+oid+'_aux a');
		for( var i = 0; i < scrollarrows.length; i++ )
		{
			if( parseInt( scrollarrows[i].innerHTML ) > numpages )
			{
				scrollarrows[i].style.display='none';
				scrollarrowsAux[i].style.display='none';
			}
			else
			{
				scrollarrows[i].style.display='inline';
				scrollarrowsAux[i].style.display='inline';
			}
		}
	});
}

function scrollPatternPage(oid,newPage)
{
	var curPage = parseInt($('#product_pattern_gallery_scrollarrows_'+oid+' .selected').html());
	var delta = newPage - curPage;
	if( delta == 0 )
		return;
	scrollPattern( oid, -delta );
}

function scrollPattern(oid,dir)
{
	var scrollerUp = document.getElementById('scroller_'+oid+'_up');
	var scrollerUpAux = document.getElementById('scroller_'+oid+'_up_aux');
	var scrollerDown = document.getElementById('scroller_'+oid+'_down');
	var scrollerDownAux = document.getElementById('scroller_'+oid+'_down_aux');

	// Snap top to nearest stop point and then move it up or down one
	var maxtop = 272 + -272 * Math.ceil($("#scroller_"+oid).children("div.product_pattern_gallery_scroll").children("span").not(".product_pattern_item_hidden").length/8);
	var newtop = Math.ceil(parseInt($("#scroller_"+oid).children("div.product_pattern_gallery_scroll").css("top")) / 272 ) * 272;
	newtop += 272 * dir;
	//alert("newtop "+newtop+" for maxtop "+maxtop);

	// Update back/forward image icons
	if( newtop >= 0 )
	{
		newtop = 0;
		scrollerUp.src = 'includes/templates/isidora/images/scroll_left_off.gif';
		scrollerUpAux.src = 'includes/templates/isidora/images/scroll_left_off.gif';
	}
	else if( maxtop < 0 )
	{
		scrollerUp.src = 'includes/templates/isidora/images/scroll_left.gif';
		scrollerUpAux.src = 'includes/templates/isidora/images/scroll_left.gif';
	}
	if( newtop <= maxtop )
	{
		newtop = maxtop;
		scrollerDown.src = 'includes/templates/isidora/images/scroll_right_off.gif';
		scrollerDownAux.src = 'includes/templates/isidora/images/scroll_right_off.gif';
	}
	else
	{
		scrollerDown.src = 'includes/templates/isidora/images/scroll_right.gif';
		scrollerDownAux.src = 'includes/templates/isidora/images/scroll_right.gif';
	}

	// Scroll box contents
	$("#scroller_"+oid).children("div.product_pattern_gallery_scroll").animate({top:newtop+'px'},500);

	// Update page numbers
	var newpage = (-( newtop / 272 )) + 1;
	var newpageA = document.getElementById('scroller_'+oid+'_page_'+newpage);
	var newpageAAux = document.getElementById('scroller_'+oid+'_page_'+newpage+'_aux');
	$('#product_pattern_gallery_scrollarrows_'+oid+' .selected').removeClass('selected');
	$('#product_pattern_gallery_scrollarrows_'+oid+'_aux .selected').removeClass('selected');
	if( newpageA )
		$(newpageA).addClass('selected');
	if( newpageAAux )
		$(newpageAAux).addClass('selected');
}
