// JavaScript Document
/* $Id: SE-util.js 2995 2008-11-28 10:29:57Z bartek $ */

// Funktion för vykort
var vykortWin = null;

function vykort( product_id, category_id )
{

	if( vykortWin != null && !vykortWin.closed) {
		vykortWin.focus();
	}
	url = "vykort/vykort.htm?product_id=" + product_id + "&category_id=" + category_id;
	vykortWin = window.open(url, "vykort", "left=0,top=0,width=585,height=530,dependent,resizable=yes");

}

// funktion för att visa originalbilder i eget fönster
function fullsize_pic( name, url, width, height )
{
	if ( width == -1 )
		width = 500;
	if ( height == -1 )
		height = 500;
		
	var left = 0;
	var top  = 0;
	var win = window.open('', 'BigPic', 'width='+width+',height='+height+',toolbar=no,menubar=no,status=no,scrollbars=no,resizable=yes');
	var ePen ='<html><head><title>'+name+'</title></head>'; 
	ePen += '<body text="black" bgcolor="white" leftmargin=0 topmargin=0 onBlur="self.close()">';
	ePen += '<img src="'+url+'">';
	ePen += '</body></html>';
	var wd = win.document;
	wd.open();
	wd.write(ePen);
	wd.close();
}


/* eMenu
 * menuExpandable2.js - implements an expandable menu based on a HTML list
 * Original Author: Dave Lindquist (dave@gazingus.org)
 * Made up to snuff by Svenskabutiker AB (martin@svenskabutiker.se)
 */

var eMenuDocument = null;
var eMenuSelected = null;
var eMenuState = new Object();
//if (!document.getElementById)
//    document.getElementById = function() { return null; }


function eMenuInit(myDoc, menuId, wantReload ) {
	eMenuDocument = myDoc;
	var menu = eMenuDocument.getElementById(menuId);
	var obj, submenu;
	if (menu == null) return;
 //   if (window.opera) return; // I'm too tired

	var elem =  menu.getElementsByTagName("a");
	for( var i=0; i < elem.length; i++ ) {
		obj = elem[i];
		if( obj.className == "actuator" ) {
			submenu = obj.parentNode.getElementsByTagName("ul")[0];
			if( eMenuState[submenu.parentNode.id] ) { // Display
				this.parentNode.className = "menubar open";
				submenu.style.display = "block";
			} else { 
				// Hide - 
				obj.parentNode.className = "menubar closed";
        		submenu.style.display = "none";
			}
			if( !wantReload )
  				obj.onclick = eMenuClick;
		}
	}
	if(eMenuSelected)
		eMenuShowSelect(eMenuSelected);
}

function eMenuClick() {
	var submenu = this.parentNode.getElementsByTagName("ul")[0];
	var display = submenu.style.display;

	if( display == "block" ) { // is open
		this.parentNode.className = "menubar closed";
//		this.parentNode.style.backgroundImage = "url(/__SYS__/plus.gif)";
		submenu.style.display = "none";
		delete eMenuState[this.parentNode.id];
	} else {
		this.parentNode.className = "menubar open";
//		this.parentNode.style.backgroundImage = "url(/__SYS__/minus.gif)";
		submenu.style.display = "block";
		eMenuState[this.parentNode.id] = 1;
	}

	return false;
}

function eMenuShowSelect( id ) {
	var obj;

	if(!eMenuDocument) return null;

	if( eMenuSelected ) 
		eMenuDocument.getElementById( eMenuSelected ).className = null;

	obj = eMenuDocument.getElementById( id );
	if(!obj) return null;

    if( obj.className == "menubar closed" ) {
		obj.className = "menubar open selected";
		submenu = obj.getElementsByTagName("ul")[0];
		submenu.style.display = "block";
	} else
		obj.className = "selected";

    eMenuSelected = id;
	while( obj.className != "menu" ) {
		if( obj.className == "submenu" ) {
			obj.parentNode.className = "menubar open";
			obj.style.display = "block";
		}
		if( obj.className == "menubar" ) {
			
			this.parentNode.className = "menubar open";
//			obj.style.backgroundImage = "url(/__SYS__/minus.gif)";
//		alert("ADD: menubar " + obj.id);
			eMenuState[obj.id] = 1;
		}
		obj = obj.parentNode;
	}
}	
/* end eMenu */

function add_favorite( variant_id )
{
	get_reload("/index.html?ADD_FAVORITE=1&variant_id="+ variant_id);
}

function delete_favorite( variant_id )
{
	get_reload("/index.html?DELETE_FAVORITE=1&variant_id="+ variant_id);
}

function add_wishlist( variant_id )
{
	$('#wishlist_variant_id').val(variant_id);
	$('#wishlist_additem_form').submit();
}

function switch_wishlist( wishlist_id )
{
	get_reload("/index.html?SWITCH_WISHLIST=1&wishlist_id="+ wishlist_id);
}

function delete_wishlist(wishlist_id)
{
	get_reload("/index.html?DELETE_WISHLIST=1&wishlist_id="+ wishlist_id);
}

function get_reload(url) {
    $.get(url, function () {
	location.reload(true);
    });
}    


/* Util functions for the variant variant groupings */
var var_group_info = {};
var var_field_info = [];
function var_clear()
{
    var_group_info = {};
    var_field_info = [];
}

function var_field(id)
{
    var_field_info.push(id);
}

function var_add(id, specif)
{
    if (var_group_info[specif] == undefined) var_group_info[specif] = []
    var_group_info[specif].push(id);
}

function var_select()
{
    var i;
    var spec = "";
    for (i in var_field_info) {
	i = var_field_info[i];
	if (spec != "") spec += ":";
	spec += $('#variant_group_field'+i).val();
    }
 
    /* Hide every variant line */
    $('.vargroupingtag').hide();
    
    /* If product found, show it */
    if (var_group_info[spec] != undefined) {
	var ids = var_group_info[spec];
	for (i in ids) {
	    $('#varrow_'+ids[i]).show();
	    $('.varrow_'+ids[i]).show();
	}
    } else {
	/* No product, show error */
	$('#var_bad_opts').show();
    }
}

function send_parent_form( whichForm, whichAction ) {
	var theForm = $(whichForm).parents('form:first');
	$(theForm).append('<input type="hidden" name="'+whichAction+'" value="1" />');
	$(theForm).submit();
}

function show_working_message() {
	if( $('#the_working_message').length ) {
		$('#the_working_message').show();
	} else {
		$('body').append('<div id="the_working_message">Working...</div>');
	}
}

function hide_working_message() {
	$('#the_working_message').hide();
}