var isIE    = !!(navigator.userAgent.toLowerCase().indexOf('msie') >= 0 && document.all);
var isOpera = !!(window.opera && document.getElementById);
var isMoz   = (!isIE && !isOpera);

Array.prototype.inArray = function (value)
{
    for (var i = 0; i < this.length; i++)
    {
        if (this[i] == value) { return true; };
    };

    return false;
};

function setSelectedOptions(select_id, values)
{
    if (typeof values != 'object')
    {
        values = [values];
    }

    var options = document.getElementById(select_id);

    for (var i = 0; i < options.length; i++)
    {
        for (var j = 0; j < values.length; j++)
        {
            options[i].selected = values.inArray(options[i].value);
        }
    }
};

function swapImage(image, source)
{
    if (source == '') return false;
    image.style.width = '80px';
    image.style.height = 'auto';
    image.src = source;
    image.onmouseover = null;
    image.onclick = function (e) {popupImage(source);}
};

var popupTimeId = null;

function popupImage(source)
{
    closeImagePopup();

    $('#site-main').append('<div id="img_popup"><a href="javascript://" onclick="closeImagePopup()">close</a><div style="clear: both"></div><img src="" id="popuped_image"></div>');
    var img = $('#popuped_image').get(0);
    img.src = source;

    if (!img.complete)
    {
        img.onload = function ()
        {
            $('#img_popup').show();
            centerElement($('#img_popup').get(0));
            popupTimeId = setTimeout(closeImagePopup, 3000); // 3 seconds for enjoy
        }
    }
    else
    {
        $('#img_popup').show();
        centerElement($('#img_popup').get(0));
        popupTimeId = setTimeout(closeImagePopup, 3000); // 3 seconds for enjoy
    }
};

function closeImagePopup()
{
    $('#img_popup').remove();
    clearTimeout(popupTimeId);;
};

function showGenericPopup(content) {
    closeGenericPopup();
    $('#site-main').append('<div id="generic_popup"><a href="javascript://" onclick="closeGenericPopup()">close</a><div style="clear: both"></div><div id="popup_contents"></div></div>');
    $('#popup_contents').html(content);
    $('#generic_popup').show();
    centerElement($('#generic_popup').get(0));
}

function closeGenericPopup() {
    $('#generic_popup').remove();
}

function centerElement(element, consider_scroll)
{
    if (!element.offsetWidth) return false;

    var left = document.body.offsetWidth / 2 - element.offsetWidth / 2;
    var top  = (isMoz ? window.innerHeight : document.documentElement.clientHeight) / 2 - element.offsetHeight / 2;

    if (consider_scroll != false)
    {
        top += (window.pageYOffset || document.documentElement.scrollTop);
    }

    element.style.position  = 'absolute';
    element.style.left      = left + 'px';
    element.style.top       = top + 'px';
};

function redirect_post (url, data) {
	var f = document.createElement('form');
	f.method = 'POST';
	f.action = url;
	for (var k in data) {
		var i = document.createElement('input');
		i.type = 'hidden';
		i.name = k;
		i.value = data[k];
		f.appendChild(i);
	}
	document.body.appendChild(f);
	f.submit();
};

var optionsHidden = true;
function toggleOptions ()
{
	if (optionsHidden)
	{
		$('#options-thumbler').removeClass('options-thubmler-collapse');
		$('#options-thumbler').addClass('options-thubmler-expand');
		$('#table_options').fadeIn();
	}
	else
	{
		$('#options-thumbler').removeClass('options-thubmler-expand');
		$('#options-thumbler').addClass('options-thubmler-collapse');
		$('#table_options').fadeOut();
	}

	optionsHidden = !optionsHidden;
};

var country_selector_hidden_margin;
var country_selector_height;

//function positionCountrySelector()
//{
//    alert($('#menu-holder').width());
//
//    var left = $('#menu-holder').width() - $('#country-selector').width() + 40;
//
//    country_selector_margin = $('#country-selector').height() - $('#country-selector-switch').height();
//
//    $('#country-selector').css('left', left + 'px');
//    $('#country-selector').css('margin-top', '-' + country_selector_margin + 'px');
//};

function toggleCountrySelector()
{
    var margin_top = parseInt($('#country_selector').css('margin-top'));

    if (margin_top == country_selector_hidden_margin)
    {
        val = (($.browser.msie && $.browser.version.indexOf('7.') > -1)) ? 18 : 10;
    }
    else
    {
        val = country_selector_hidden_margin;
    }

    $('#country_selector').animate({marginTop: val}, 'slow');
};

$(document).ready
(
    function()
    {
        $('#country_selector').css('visibility', 'hidden');
        $('#country_selector').css('display', 'block');

        country_selector_height = $('#country_selector').height();

        var m1 = country_selector_height + 12;
        var m2 = $('#country_selector > .bottom').height();
        country_selector_hidden_margin = -(m1 - m2);

        var left_corr = 77;

        if ($.browser.msie && $.browser.version.indexOf('7.') > -1)
        {
            country_selector_hidden_margin += 8;
            var left_corr = 60;
        }
        else if ($.browser.msie)
        {
            country_selector_hidden_margin += 14;
            var left_corr = 80;
        }

        $('#country_selector').css('left', findPos($('#header_content_right').get(0))[0] + left_corr);

        $('#country_selector').css('margin-top', '-' + $('#country_selector').height() + 'px');
        $('#country_selector').css('visibility', 'visible');
        $('#country_selector').animate
        (
            { marginTop : country_selector_hidden_margin },
            500
        );

        if (typeof (CURRENT_CATEGORY) != 'undefined' && CURRENT_CATEGORY != '')
        {
            $('#category_switch').attr('disabled', false).attr('checked', true).show();
        }
    }
);

function _parseInt(value)
{
    return isNaN(parseInt(value)) ? 0 : parseInt(value);
};

function findPos(obj)
{
    if (typeof obj == 'undefined') return false;

	var curleft = curtop = 0;

	if (obj.offsetParent)
	{
		curleft = obj.offsetLeft;
		curtop  = obj.offsetTop;

		while (obj = obj.offsetParent)
		{
			curleft += obj.offsetLeft;
			curtop  += obj.offsetTop;
		}
	}

	return [curleft,curtop];
};

function edit_dynatext(key)
{
    window.open(BASE_URL + '/edit_dynatext.php?key=' + key, 'dynatext', 'width=400,height=600,scrollbars=yes');
};

function toggleTagsGroup(id)
{
    if ($('#' + id).css('display') == 'none')
    {
        $('#' + id).slideDown(200);
        $('#tags_toggle_' + id).html('&laquo;');
    }
    else
    {
        $('#' + id).slideUp(200);
        $('#tags_toggle_' + id).html('&raquo;');
    }
};

function desc(image_url, name, description)
{
    $('#big_pic').attr('src', image_url);
    $('#big_pic').css('display', 'block');
    $('#product_name').html(name);
    $('#product_description').html(description);
};

function dropTr(btn)
{
	if(document.getElementById)
	{
		tr = btn;
		while (tr.tagName != 'TR') tr = tr.parentNode;
		tr.parentNode.removeChild(tr);
		checkForLast();
	}
};

function addTr(btn)
{
	if(document.getElementById)
	{
		tr = btn;

		while (tr.tagName != 'TR')
		{
		    tr = tr.parentNode;
		}

		var idSuffix = Math.round(Math.random()*1000);

		var new_node = tr.cloneNode(true);

		var newTr = tr.parentNode.insertBefore(new_node, tr.nextSibling);
		checkForLast();
	}
};

function checkForLast()
{
	btns = document.getElementsByName('drop');
	for (i = 0; i < btns.length; i++)
	{
		btns[i].disabled = (btns.length == 1) ? true : false;
	}
};
