function popkorn (path)
{
    window.open(path, 'popupwindow','width=600,height=500,scrollbars,resizable');
    return false;
}
function showsearch ()
{
    var searchbox = document.getElementById('searchbox');
    if (searchbox.style.display == 'none') {
        searchbox.style.display = 'block';
        searchbox.style.visibility = 'visible';
        var sinput = document.getElementById('sinput');
        sinput.focus();
    } else {
        searchbox.style.display = 'none';
        searchbox.style.visibility = 'hidden';
    }
}
function show_friendbox ()
{
    var friendbox = document.getElementById('friendbox');
    if (friendbox.style.display == 'none') {
        friendbox.style.display = 'block';
        friendbox.style.visibility = 'visible';
        //var sinput = document.getElementById('sinput');
        //sinput.focus();
    } else {
        friendbox.style.display = 'none';
        friendbox.style.visibility = 'hidden';
    }
}
function send_friend ()
{
    var friend_email = dgi('friend_email').value;
    var your_email = dgi('your_email').value;
    var friend_name = dgi('friend_name').value;
    
    var vara = dgi('vara').value;
    
    request('option=com_ahsshop&do=send_friend&no_html=1&friend_email='+friend_email+'&your_email='+your_email+'&friend_name='+friend_name+'&vara='+vara);


}
function dgi (id)
{
    return document.getElementById(id);
}
function createRequestObject() {
    var ro;
    var browser = navigator.appName;
    if(browser == "Microsoft Internet Explorer"){
        ro = new ActiveXObject("Microsoft.XMLHTTP");
    }else{
        ro = new XMLHttpRequest();
    }
    return ro;
}

var http = createRequestObject();

function request(query_string)
{
    document.getElementById('msg').innerHTML = 'sendi...';
    http.onreadystatechange = handleResponse;
    /* ŽETTA ER FYRIR GET
    http.open('get', 'rpc.php?'+query_string);
    http.send(null);
    */

    /* try instead */
    http.open('POST', 'index2.php', true);
    http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    http.setRequestHeader("Content-length", query_string.length);
    http.setRequestHeader("Connection", "close");
    http.send(query_string);
}

function handleResponse() {

    var response = http.responseText;
    if(http.readyState == 4){
        document.getElementById('msg').innerHTML = response;
    }
}

$(document).ready(function(e) {
	$('.icon').bind('mouseenter', function(e) {
        var what = $(this).attr('tag');
        $(this).attr('src','/media/icon/'+what+'_over.gif');
    })
	$('.icon').bind('mouseleave', function(e) {
        var what = $(this).attr('tag');
        $(this).attr('src','/media/icon/'+what+'.gif');
    })
    $('.autosubmit').change(function(e) {
        $('#'+$(this).attr('rel')).submit()
    })    
})

function setimg(tag, click) {
	//var tag = $(this).attr('tag')
	var sel = $('select option[tag="'+tag+'"]')
	sel.attr('selected', 'selected')
	var val = sel.val();
	//$('.selectbox-wrapper li').removeClass('selected')
	//alert($('.selectbox-wrapper #_input_'+val).html()+ " "+'.selectbox-wrapper #_input_'+val)
	//$('.selectbox-wrapper #_input_'+val).addClass('selected')
	if(click) {
		$('.selectbox-wrapper #_input_'+val).click();
	}
	var litaimg = $('#extraimg img[rel='+tag+']');
	if(litaimg.length>0) {
		$('#extraimg img').removeClass('selected')
		litaimg.addClass('selected')
		$('.litatag').removeClass('selected')
		$('.litatag[tag='+tag+']').addClass('selected')
		var src = litaimg.eq(0).attr('src')    
		var mynd = litaimg.eq(0).attr('mynd')    
		$('img.vorumynd').attr({'src' : src, 'mynd' : mynd})
	}
}
$(function () {
    /*$('.mynd > *').bind('mouseenter mouseout mouseleave mouseover', function(e) {
        e.preventDefault()
    })*/
    $('.efnistrigger').click(function(e) {
        var info = $('#intro'+$(this).attr('sqlid'));
        var vis = info.attr('visible') ? info.attr('visible'): 0;
        if(info.attr('visible')==1) {
            //info.hide()
            info.slideUp();
            info.attr('visible', 0)
        } else {
            //info.show();
            info.attr('visible',1)
            info.slideDown();
        }
    })
    /*
    $('.mynd a').lightBox({
        fixedNavigation:true,
        overlayBgColor: '#555',
        imageLoading: 'components/com_ahsshop/lb_images/loading.gif',
        imageBtnPrev: 'components/com_ahsshop/lb_images/prev.gif',
        imageBtnNext: 'components/com_ahsshop/lb_images/next.gif',
        imageBtnClose: 'components/com_ahsshop/lb_images/loka.gif',
        imageBlank: 'components/com_ahsshop/lb_images/blank.gif'
    });*/
	$('.colors .selectbox-wrapper li').livequery(function() {
			var p = $(this).closest('.colors');
			$(this).click(function() {
				var val = $(this).attr('id').substr(7)
				var tag = p.find('option[value='+val+']').attr('tag')
				setimg(tag, 0);
			})
	})
    
    $('.bubbleInfo').livequery(function() {
      $(this).each(function () {
        // options
        var left = 0;
        var time = 450;
        var hideDelay = 100;

        var hideDelayTimer = null;

        // tracker
        var beingShown = false;
        var shown = false;
        
        var trigger = $('.trigger', this);
        var distance = trigger.attr('distance') ?  trigger.attr('distance') : -200;
        var popup = $('.popup', this).css('opacity', 0);
        // set the mouseover and mouseout on both element
        $('#extraimg img').click(function() {
				setimg($(this).attr('rel'), 1)
		})
        trigger.click(function(e) {
			setimg($(this).attr('tag'), 1)
        })
        if($(this).hasClass('pop')) {
            $([trigger.get(0), popup.get(0)]).mouseover(function () {
              // stops the hide event if we move from the trigger to the popup element
              if (hideDelayTimer) clearTimeout(hideDelayTimer);

              // don't trigger the animation again if we're being shown, or already visible
              if (beingShown || shown) {
                return;
              } else {
                beingShown = true;

                // reset position of popup box
                
                popup.css({
                  top: -popup.height()-distance,
                  left: -(popup.width()/2)+left ,
                  display: 'block',
                  opacity: 0
                })

                // (we're using chaining on the popup) now animate it's opacity and position
                .animate({
                  opacity: 1
                }, time, '', function() {
                  // once the animation is complete, set the tracker variables
                  beingShown = false;
                  shown = true;
                });
              }
            }).mouseout(function () {
              // reset the timer if we get fired again - avoids double animations
              if (hideDelayTimer) clearTimeout(hideDelayTimer);
              
              // store the timer so that it can be cleared in the mouseover if required
              hideDelayTimer = setTimeout(
              function () {
                hideDelayTimer = null;
                popup.animate({
                  opacity: 0
                }, time, 'swing', function () {
                  // once the animate is complete, set the tracker variables
                  shown = false;
                  // hide the popup entirely after the effect (opacity alone doesn't do the job)
                  popup.css('display', 'none');
                });
              }, hideDelay);
            });
        }
      });
    });
});  
