var opera = (navigator.userAgent.indexOf('Opera') >= 0)? true : false;
var chrome = (navigator.userAgent.indexOf('Chrome') >= 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';
}

function isEmpty(s)
{
    if (s != null ) {
        em = s.replace(/[\t\r\n ]*/g, '' );
    }
    
    return (s == null || s == '' || em.length == 0);
}

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 (opera && win) {
    	win.close();
    	win = false;
    }
    if(!win || win.closed) {
        xy = '';
        if(center) {
            x  = (screen.availWidth-w)/2;
            y  = (screen.availHeight-h)/2;
            xy = ',top='+y+',left='+x;
        }
        if (chrome) {
        	win = window.open(url, name, 'toolbar=0,status=0,resizable=0,scrollbars='+(scrollbars?1:0)+',width='+w+',height='+h+xy).blur();
        }
        else {
        	win = window.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));
            counter.text('0');
        } else {
            counter.text(maxlimit - length);
        }
    });
}

function item(id) { ow('/showitem.php?id='+id+'&sw='+clWidth+'&sh='+clHeight, 'viewer', 280, 120, true); }
function photo(id) { item(id); } 

var opera = (navigator.userAgent.indexOf('Opera') >= 0)? true : false;
var ie    = (document.all && !opera)? true : false;
var dom   = (document.getElementById)? true : false;

function addToFavorites(url, title)
{
    try {
	    if (window.sidebar) {
	        window.sidebar.addPanel(title, url, '');
	    } else if (window.opera && window.print) {
	        var e = document.createElement('a');
	        e.setAttribute('rel', 'sidebar');
	        e.setAttribute('href', url);
	        e.setAttribute('title', title);
	        e.click();
	    } else if (document.all) {
	        window.external.AddFavorite(url, title);
	    }
    } catch (e) {
    }
}

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 showSignupComment() { 
        ow('signupComment.php', 'comment', 500, 300, 1);
}

window.onload = onLoadHandler;

function trim(str){
	return str.replace(/^\s*([\S\s]*?)\s*$/, '$1');
}

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 hexColor(color) {
    isHex = color.match(/^\#.*/);
    if (isHex) {
        return color;
    } else {
        color = color.match(/^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/);
        function hex(x) {
            hexDigits = new Array("0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f");
            return isNaN(x) ? "00" : hexDigits[(x - x % 16) / 16] + hexDigits[x % 16];
        }
        return "#" + hex(color[1]) + hex(color[2]) + hex(color[3]);
    }
}

function buyItemLink(id, text){
	var conf;
	if (conf = confirm(text)){
		document.getElementById('itemLinkID'+id.toString()).href = "javascript:item(" + id.toString() + ")";
	}
	return conf;
}

function removeWebmasterAccount(message, okbutton, cancelbutton)
{
    if (confirm(message)) {
        jQuery('#removeAccountConfirm').dialog({
            modal: true,
            draggable: true,
            width: '400px',
            buttons: [{
                text: okbutton,
                click: function() {
                    jQuery('#removeAccountConfirm form').submit();
                }
            }, {
                text: cancelbutton,
                click: function() {
                    jQuery('#removeAccountConfirm').dialog('close');
                }
            }]
        });
    }
    return false;
}

function byItemClick(id, text){
    var conf;
    if (conf = confirm(text)) {
        document.getElementById('itemID'+id.toString()).onclick = function() { showItem(id); };
        showItem(id);
    }
    return conf;
}

function showItem(id)
{
    ow('/showitem.php?id=' + id + '&sw=' + clWidth + '&sh=' + clHeight, 'viewer', 280, 120, true);
}

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).show();
                }
            },
            'text'
     );
}
(function($){
	$.fn.addCheckboxRelation = function(selectorCheckbox)
	{
		var checkbox = $(selectorCheckbox);
		
        if (!checkbox.attr('checked')) {
        	$(this).attr('disabled', true);
        }
        
        checkbox.click(function(input){
        	return function(){
        		$(input).attr('disabled', !$(this).attr('checked'));
        	}
        }(this));
	}
})(jQuery)

function showLoadImgTo(obj) {
	if (typeof obj.loadImg == 'undefined') {
		obj.loadImg = $('<img>').attr('src', '/i/ajax-loader.gif'); 
		obj.loadImg.css({
			'margin': '-32px 0 0 -32px',
			'position': 'relative',
			'left': '50%',
			'top': '50%'
		})
		$(obj).append(obj.loadImg);
	}
	
	obj.loadImg.show();
}

function hideLoadImgTo(obj) {
	if (typeof obj.loadImg != 'undefined') {
		obj.loadImg.hide();	
	}
}

function makeDialog(o) {
	var o = $.extend({
		url: false,
		htmlObj: false,
		width: 1000,
		height: 600,
		modal: true
	}, o),
		funcObj = arguments.callee;

    if (typeof funcObj.dialogWindow == 'undefined') {
        funcObj.dialogWindow = $('<div class="jDialogWindow">')
            .appendTo($('body'));
        funcObj.content = $('<div>');
        funcObj.dialogWindow.append(funcObj.content);
        
    }
    funcObj.content.empty();
    funcObj.content.hide();

	funcObj.dialogWindow.dialog("destroy");
	if (o.url) {
		showLoadImgTo(funcObj.dialogWindow);
		$.get(o.url, {}, function(data) {
			hideLoadImgTo(funcObj.dialogWindow);
			funcObj.content.append(data);
			funcObj.content.show();
		})
	} else if (o.htmlObj) {
		funcObj.dialogWindow.empty();
		funcObj.dialogWindow.append(o.htmlObj);
	}
    
    return funcObj.dialogWindow.dialog(o); 
}

function dialogConfirm(o)
{
	o = $.extend({
		title: 'Confirm',
		text: 'Are you sure?',
		callback: function(){},
		width: 350,
		height:	170,
		buttonOkText: 'Ok',
		buttonCancelText: 'Cancel'
	}, o);
	
	o.buttons = [{
		text: o.buttonOkText,
		click: function() {
			o.callback()
			$(this).dialog("close");
		}
	}, {
		text: o.buttonCancelText,
		click: function() {
			$(this).dialog("close");
	    }
	}]
	
	o.htmlObj = $('<p class="confirm-text">').html(o.text);
	
	makeDialog(o);
}

function changePasswordDialog(rivcash)
{
    if ($('#passwordDialog').size() == 0) {
        $.get('/ajax/' + (rivcash ? 'rivcashPasswordChangeBox.php' : 'passwordChangeBox.php'), {}, function(data) {
            $('body').append(data);
            changePasswordDialog(rivcash);
        });
    } else {

        $('#passwordDialog input[name*=ass]').val(''); // ass - oldPass, pass and pass2
        $('#passwordDialog .message').empty();
        $('#passwordDialog').dialog({
            modal: true,
            width: '500px',
            buttons: {
                'Save': function(){
                    $('#passwordDialog .message').empty();
                    $('.ui-dialog-buttonpane').append('<img class="ajax-loader" src="/i/ajax-loader.gif"/>');
                    $(this).find('form').ajaxSubmit(function(data) {
                        $('.ui-dialog-buttonpane img.ajax-loader').remove();
                        data = jQuery.parseJSON(data);
                        if (data) {
                            if (data.errorMsg) {
                                $('#passwordDialog .message').html(data.errorMsg);
                            }
                        } else {
                            $("#passwordDialog").dialog('close');
                            $("#passwordChangedOk").dialog({
                                modal: true,
                                buttons: {
                                    Ok: function() {
                                        $(this).dialog("close");
                                    }
                                }
                            });
                        }
                    });
                },
                'Cancel': function(){$(this).dialog('close');}
            }
        });
    }
    return false;
}

function checkShowageField()
{
	var status = $('#showage').val();
	switch (status) {
	    case '1':
	        $('#advertisedAge').attr('disabled', false);
	        $('#advertisedAge').parent().parent().show();
	        break;
	    default:
	        $('#advertisedAge').attr('disabled', true);
	    	$('#advertisedAge').parent().parent().hide();
	}
}
