//	------------------------------------
//
//	Basic tool scripts for EasyFlow Shop
//
//	------------------------------------

// Namespace
var EZF = {};

// Toggle visiblity of one or two HTML elements

function toggleElement(id1)
{
	if (document.getElementById)
	{
		var el1 = document.getElementById(id1);
		if (el1 != null) el1.style.display = (el1.style.display != 'block') ? 'block' : 'none';
	}
}

function toggle(id1, id2)
{
	if (document.getElementById)
	{
		var el1 = document.getElementById(id1);
		var el2 = document.getElementById(id2);			
		if (el1 != null) el1.style.display = (el1.style.display != 'block') ? 'block' : 'none';
		if (el2 != null) el2.style.display = (el2.style.display != 'block') ? 'block' : 'none';	
	}
}

// Toggle the content of an image
function toggleImages(target, picture)
{
	document.getElementById(target).src = picture
}

// Set "alternate" class on every odd table row in tables with a class = tableClass.
function alternateTable(tableClass)
{
	var i, n, tables, table, trs, tr;
	var re = new RegExp("\\b" + tableClass + "\\b");
	tables = document.getElementsByTagName("table");
	for (i = 0; (table = tables[i]); i++)
	{
		if (re.exec(table.className))
		{
			var tbodys = table.getElementsByTagName("tbody");
			if (tbodys.length == 1)
			{
				trs = tbodys[0].getElementsByTagName("tr");
			}
			else
			{
				trs = table.getElementsByTagName("tr");
			}

			for (n = 0; (tr = trs[n]); n++)
			{
				if (n % 2 == 1)
				{
					tr.className += " alternate";
				}
				else
				{
					tr.className += " regular";
				}
			}	
		}
	}
}

// Used for the ProductTypePicker... IE workaround only

function mouseOverHandler()
{
	if (!window.event) return;

	var elm = window.event.srcElement;
	if (typeof(elm.q) == "undefined" ){ elm.q = elm.style.width; }
	
	var orgWidth = elm.offsetWidth;
	elm.style.width = 'auto';
	if (elm.offsetWidth < orgWidth) elm.style.width = elm.q;

	elm.onmouseleave = function() {
		elm.style.width = elm.q;
	}
}

function focusHandler()
{
	if (!window.event) return;
	
	var elm = window.event.srcElement;

	elm.onmouseenter = null;
	elm.onmouseleave = null;
 
	elm.onblur = function() {
		elm.style.width = elm.q;
	}
}

/* Cookie Helper
*/
var Cookie = {
	get: function(name) {
		name += "=";
		var alen = name.length;
		var clen = document.cookie.length;
		var i = 0;
		while (i < clen) {
			var j = i + alen;
			if (document.cookie.substring(i, j) == name) return Cookie._getValue(j);
			i = document.cookie.indexOf(" ", i) + 1;
			if (i == 0) break;
		}
		return null;
	},
	set: function(name, value) {
		var cookie = name + '=' + escape(value);
		var options = arguments[2] || {};
		if (options.expires) cookie += '; expires=' + options.expires.toGMTString();
		if (options.path) cookie += '; path=' + options.path;
		if (options.domain) cookie += '; domain=' + options.domain;
		if (options.secure) cookie += '; secure';
		document.cookie = cookie;
	},
	_getValue: function(offset) {
		var endstr = document.cookie.indexOf(";", offset);
		if (endstr == -1) endstr = document.cookie.length;
		return unescape(document.cookie.substring(offset, endstr));
	}
};

/* Set anchors with class name = "external" to open in a new window.
*/
if (typeof Event != 'undefined' && Event.observe) {
	Event.observe(window, 'load', function() {
		$$('a.external').each(function(el){el.target = '_blank';});
	});
}

if (typeof Object.extend == 'function') { // only available when Prototype is loaded

/* Pop-up image.
* Usage: new EZF.PopupImage('ClientID', { href: '/path/to/popup.html', width: 123, height: 456 });
*/
EZF.PopupImage = function(id) {
  this.img = $(id);
  this.options = Object.extend(EZF.PopupImage.options, arguments[1] || {});
  if (!this.options.href) 
  {
    this.options.href = this.img.src;
  }
  this.imgMouseOverHandler = EZF.PopupImage.mouseover.bindAsEventListener(this);
  this.imgMouseOutHandler = EZF.PopupImage.mouseout.bindAsEventListener(this);
  this.imgClickHandler = EZF.PopupImage.click.bindAsEventListener(this);
  
  this.img.observe('mouseover', this.imgMouseOverHandler);
  this.img.observe('mouseout', this.imgMouseOutHandler);
  this.img.observe('click', this.imgClickHandler);
 
};

Object.extend(EZF.PopupImage, {
  options: { tooltip: 'Klik for at se billedet i fuld størrelse', width: 400, height: 400 },
  layer: null,
  mouseover: function(e) {
    var layer = EZF.PopupImage.layer;
    if (!layer) {
      layer = $(document.createElement('div'));
      layer.id = 'EZF_PopupImage_layer';
      layer.className = 'tooltip';
      layer.setStyle({'border': 'solid 1px #ccc', 'color': '#999', 'background-color': '#fff', 'padding': '5px', 'position': 'absolute'});
      layer.setOpacity(0.666);
      EZF.PopupImage.layer = layer;
    }
    layer.update(this.options.tooltip);

    var pos = Position.positionedOffset(this.img);
    layer.setStyle({top: (pos[1]+5) + 'px', left: (pos[0]+5) + 'px'});

    this.img.parentNode.appendChild(layer);
  },
  mouseout: function(e) {
    var layer = EZF.PopupImage.layer;
    if (layer && layer.parentNode) {
      layer.remove();
    }
  },
  click: function(e) 
  {
    var features = new Template('status=1,width=#{width},height=#{height}').evaluate(this.options);
    window.open(this.options.href, '_blank', features);
  }
});

} // end if

function SwapImageTEMP(newImageClientId, targetImageClientId, sizeOfThumbs, sizeOfNormal, actualImageSize, showImageUrl, usingLightbox)
{
    var targetImageSrc = $(targetImageClientId).src;        
    
    $(targetImageClientId).src = SetSizeToImageUrl($(newImageClientId).src, sizeOfNormal);
    $(newImageClientId).src = SetSizeToImageUrl(targetImageSrc, sizeOfThumbs);
    
    debugger;
    if(usingLightbox == "True")
    {
        var newImageAnchorHref = $(newImageClientId).parentNode.href;
        $(newImageClientId).parentNode.href = newImageAnchorHref;
        $(targetImageClientId).parentNode.href = $(newImageClientId).parentNode.href;
    }   
    else
    {
        if(PopupImage != null)//If popup has been setup once, clear handlers
        {
           Event.stopObserving($(PopupImage.img), 'mouseover', PopupImage.imgMouseOverHandler);
           Event.stopObserving($(PopupImage.img), 'mouseout', PopupImage.imgMouseOutHandler);
           Event.stopObserving($(PopupImage.img), 'click', PopupImage.imgClickHandler);
        }
        
        PopupImage = new EZF.PopupImage(targetImageClientId, { 
                href: showImageUrl,                 
                width: actualImageSize.width,
                height: actualImageSize.height
            });   
    }
}

var FlashImage;
function SwapImage(newImageClientId, targetImageClientId, sizeOfThumbs, sizeOfNormal, postbackUrl, useResize)
{   
    if( $(targetImageClientId) == null)
    {
        window.location = postbackUrl;
    }
    else if (FlashImage != null && $(newImageClientId).parentNode.href.match(FlashImage) != null)
    {
        window.location = FlashImageUrl;
    }
    else
    {
        var targetImageSrc = $(targetImageClientId).src;
        var targetAnchorHref = $(targetImageClientId).parentNode.href;
        
        $(targetImageClientId).src = SetSizeToImageUrl($(newImageClientId).src, sizeOfNormal, useResize);
        if($(newImageClientId).parentNode.href != null)
        {
            $(targetImageClientId).parentNode.href = $(newImageClientId).parentNode.href;
            $(targetImageClientId).alt = $(newImageClientId).alt;
            $(targetImageClientId).title = $(newImageClientId).title;
        }
        
        $(newImageClientId).src = SetSizeToImageUrl(targetImageSrc, sizeOfThumbs, useResize);
        if(targetAnchorHref != null)
        {
            $(newImageClientId).parentNode.href = targetAnchorHref;
            $(newImageClientId).alt = $(targetImageClientId).alt;
            $(newImageClientId).title = $(targetImageClientId).title;
        }    
    }
}

function SetSizeToImageUrl(imageUrl, size, useResize)
{
    var result=imageUrl;
    var parts = imageUrl.split('/');
    
    if(imageUrl.match("getfile") == null)
    {
        var newSize = size.width+'x'+size.height;
        return result.replace(parts[parts.length-2], newSize);
    }
    else
    {
        return imageUrl;
    }
}