var opera = (navigator.userAgent.indexOf('Opera') >= 0)? true : false;
var ie    = (document.all && !opera)? true : false;
var dom   = (document.getElementById)? true : false;

function getStyle(itemID) {
    if(dom) {
        return document.getElementById(itemID);
    } else if(ie) {
        return document.all[itemID];
    }
}

function selectFolder(folderID) {
    folder = cookies.readCookie(folderID);
    swFolder(folderID, folder ? "none" : "block");
    if(folder) {
        cookies.deleteCookie(folderID);
    } else {
        cookies.saveCookie(folderID, 1);
    }
}

function swFolder(folderID, value) {
    getStyle(folderID).style.display = value;
}

function swDisplay(folderID) {
    getStyle(folderID).style.display = getStyle(folderID).style.display == 'none' ? '' : 'none';
}

var clWidth = (ie) ? 'document.body.clientWidth' : 'window.innerWidth';
var clHeight = (ie) ? 'document.body.clientHeight' : 'window.innerHeight';

function onLoadHandler() {
	clWidth = eval(clWidth);
	clHeight = eval(clHeight);
}

function isEmpty(s) {
	if(s != null) {
		em = s.replace( /[\t\r\n ]*/g, '' );
		return (s == null || s == '' || em.length == 0);
	}
}

var ql = {login:0, pass:0};
function checkLoginFields() {
    f = document.qlForm;
    return !isEmpty(f.login.value) && !isEmpty(f.pass.value);
}

function doLogin() {
    qf = document.qlForm;
    qf.login.value = document.ql1.login.value;
    qf.pass.value = document.ql2.pass.value;
    qf.remember.value = document.ql2.remember.checked ? 1 : 0;

    if(checkLoginFields()) qf.submit();
}

function ow(url, name, w, h, center, scrollbars) {
    if(url) url = unescape(url);
    eval('if(typeof win_'+name+' != \'undefined\') win = win_'+name+'; else win = false;');
    if(!win || win.closed) {
        xy = '';
        if(center) {
            x  = (screen.availWidth-w)/2;
            y  = (screen.availHeight-h)/2;
            xy = ',top='+y+',left='+x;
        }
        win = open(url, name, 'toolbar=0,status=0,resizable=0,scrollbars='+(scrollbars?1:0)+',width='+w+',height='+h+xy);
        eval('win_'+name+' = win;');
    }
    else
    {
        win.focus();
        if(url && win.location.pathname + win.location.search != url) win.location = url;
    }
}

function textCounter(fieldId, counterId, maxlimit) 
{
    var textarea = $('#' + fieldId);
    var counter = $('#' + counterId);
                
    textarea.bind('keyup', function()
    {
        var length = textarea.attr('value').length;
        var value = textarea.attr('value');

        if (length > maxlimit) {
            textarea.attr('value', value.substring(0, maxlimit))
        } else {
            counter.text(maxlimit - length);
        }
    });
}

function disableButtonIfEmpty (fieldId, buttonId) {
    var field = $('#' + fieldId);
    var button = $('#' + buttonId);
    
    field.bind('keyup', function() {
        if (field.attr('value').length == 0) {
            button.attr('disabled', 'disabled');
        } else {
            if(button.attr('disabled')) {
                button.removeAttr('disabled');
            }
        }
    });
}

function byItemClick(id, text){
	var conf;
	if (conf = confirm(text)){
		document.getElementById('itemID'+id.toString()).onclick = function() { showItem(id)}; 
		showItem(id);		
	}
	return conf;
}

function buyVideoClick(id, text){
	var elem = null;
	var conf;
	if (conf = confirm(text)){
		document.getElementById('imgId'+id.toString()).onclick = ""; 
		document.getElementById('titleId'+id.toString()).onclick = "";
		if (elem = document.getElementById('watchItId'+id.toString())){
			elem.onclick = "";
		}	
	}
	return conf;
}

function showItem(id)
{
	ow('/showitem.php?id=' + id + '&sw=' + clWidth + '&sh=' + clHeight, 'viewer', 280, 120, true);
}

function buyItemLink(id, text){
	var conf;
	if (conf = confirm(text)){
		document.getElementById('itemLinkID'+id.toString()).href = "javascript:showItem(" + id.toString() + ")"; 		
	}
	return conf;
}

function showItemStory(id, showVoted)
{
    ow('/showitem.php?id=' + id + '&sw=' + clWidth + '&sh=' + clHeight, 'viewer', 280, 120, true);
    $.get(
            '/ajax/isVotedForEroticStory.php',
            {'id': id},
            function (data) {
                if (data == 'true' && !showVoted) {
                    $('#votestory' + id).css({'display': 'block'});
                }
            },
            'text'
     );
}
