// 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);
}

/*start Wishlist */
function add_wishlist( variant_id )
{
	$('#wishlist_selector').show();
	$('#standardoverlay').show();
	$('#wishlist_variant_id').val(variant_id);
}

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 changename_wishlist(wishlist_id,wishlistname){
	$('#changename_wishlist_id').val(wishlist_id);
	$('#changename_wishlistname').val(wishlistname);
	$('#changename_wishlistname_info').text(wishlistname);
	$('#form_changename_wishlist').show();
	$('#standardoverlay').show();
}

/*end Wishlist*/

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();
}

function OpenURLinFacebox(url){
	jQuery.facebox('<iframe src ="'+url+'" width="500px" height="550px" style="border:none;"></iframe>');
}

/*CAPTCHA BEGIN*/
function captcha_update(boxid,inputvalue,counter)
{
	$("#"+boxid).val(inputvalue);
	for (i=0;i<=4;i=i+1)
	{
		if(i==counter){
			$("#captcha-"+i).addClass("opacity");
		}else{
			$("#captcha-"+i).removeClass("opacity");
		}
	}
}

function DisplayCaptcha(div_id,form_id){
	$("#"+div_id).css("display","block");
	$("#"+div_id).load('/',{'captcha_uniqid':div_id,'captcha_form_id':form_id,'RUN_INCLUDE':'captcha_image.htm'});
}
/*CAPTCHA END*/

/*MOBILE START*/
function update_expand_bar(divid,forced){
	if($('#'+divid+'-plus').is(':hidden') && (!forced || forced=='hide')){
		$('#'+divid).hide();
		$('#'+divid+'-plus').show();
		$('#'+divid+'-minus').hide();	
	}else{
		$('#'+divid).show();
		$('#'+divid+'-plus').hide();
		$('#'+divid+'-minus').show();
	}
}
/*MOBILE END*/

/* Search suggest feature */
var search_is_running = 0;
function search_suggest(elem, url, param)
{
    if (search_is_running)
	return true;
    if (elem.value.length < 3)
	return true;

    search_is_running = 1;
    $.ajax({
	type: "GET",
	url: "/?JSON=search_suggestions&search="+escape(elem.value),
	dataType: "json",
	cache: true,
	success: function (data) {
	    s_sug_display(data, elem, url, param);
	}
    });

    return true;
}

function s_sug_display(data, elem, url, param)
{
    $(".suggested-search-strings").remove();
    var html = '<div class="suggested-search-strings"';

    if (data.length) {
	/* Get position */
	var pe = $(elem).parents('form:first');
	if (!pe) pe = elem;
	var pos = $(pe).position();
	var top = pos.top+$(pe).height();
	if ($(elem).attr("data-suggest-offs-y")) {
	    top += parseInt($(elem).attr("data-suggest-offs-y"));
	}
	var left = pos.left;
	if ($(elem).attr("data-suggest-offs-x")) {
	    left += parseInt($(elem).attr("data-suggest-offs-x"));
	}
	
	
	html += 'style="position:absolute;';
	html += "top: "+top+"px; "
	html += "left: "+left+"px; "
	html += '">'
	
	/* Write out suggestions */
	html += "<ol>\n";
	for (i in data) {
	    var res = data[i];
	    html += '<li><a href="'+url+"?"+param+"="+escape(res["searchstr"])+'">';
	    html += res["searchstr"];
	    html += '</a></li>\n';
	}
	html += "</ol>\n";	
	html += '</div>'
	
	$(elem).after(html);
    }

    search_is_running = 0;
}
