function slideFadeToggle(speed, easing, callback) {
    return $('.chathost-info', this).animate({opacity: 'toggle', height: 'toggle'}, speed, easing, callback);
}

function thispg(pg) {
    var page = pg + '.html';
    return (window.location.href.indexOf(page) !== -1) ? true : false;
}

function createCookie(name, value, days) {
    var date, expires;
    if (days) {
        date = new Date();
        date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
        expires = "; expires=" + date.toGMTString();
    } else {
        expires = "";
    }
    document.cookie = name + "=" + value + expires + "; path=/";
}

function readCookie(name) {
    var nameEQ, ca, i, c;
	nameEQ = name + "=";
	ca     = document.cookie.split(';');
	for (i = 0; i < ca.length; i++) {
		c = ca[i];
		while (c.charAt(0) === ' ') {
            c = c.substring(1, c.length);
        }
		if (c.indexOf(nameEQ) === 0) {
            return c.substring(nameEQ.length, c.length);
        }
	}
	return null;
}

$(function(){
    var a, cls, url, w, h, parm, pHeight, toTop, faq, itabcont, srchcont, clsAct, nEl, active, isSingle;
    a        = $('a');
    toTop    = '<p class="back-to-top"><a href="#top">Back to Top</a></p>';
    itabcont = '#header #header-nav ul';
    srchcont = 'dl#toggle-search';
    clsAct   = 'active';
    faq      = $('dl.faqa');
    active   = true;
    isSingle = null;

    $('img, a').not('.f').removeAttr('title');
    
    $($('[@rel$="external"]'), a).click(function () {
        window.open(this.href);
        return false;
    });

    //class="popup h:300 w:400"
    $($('.popup'), a).click(function () {
        cls  = $(this).attr('class');
        url  = $(this).attr('href');
        w    = ((cls.match(/w:(\d+)/) || [])[1]) || 320;
        h    = ((cls.match(/h:(\d+)/) || [])[1]) || 200;
        parm = "width=" + w + ", height=" + h + ", scrollbars=1, resizable=1";
        window.open(url, 'InfoWindow', parm).focus();
        return false;
    });
    
    if (thispg('faq')) {
        faq.each(function () {
            $('dt', this).each(function (i) {
                nEl = $(this).next('dd');
                $('<strong />').html(i + 1 + '. ').prependTo($(this));
                $(toTop).appendTo(nEl);
            });
        });
    }

    $('input[title!=""]:not(.skipToolTip),select[title!=""]:not(.skipToolTip),a.f,li.f,img.f').tipsy({trigger: 'focus', gravity: 'w'});

    $('#header #username, #header #Password1').focus(function () {
        this.value = "";
    });

    $('#exhibprofile .toggle-gallery').click(function () {
        pHeight = $('#profdcont #pcont1d').height();
        $('#profdcont #pcont2d').height(pHeight);
        $('#profile-menu li.toggle-gallery').toggleClass('selected');
        $('#profdcont > div').toggle();
        return false;
    });
    
    $('#viewervideosessionclose ul.voting li').click(function () {
        $('.active').removeClass(clsAct);
        $(this).addClass(clsAct);
        $('a', this).addClass(clsAct);
    });

    $('.chathost-list .chathost-picture, .wioo li, .wobb li').hoverIntent(slideFadeToggle, slideFadeToggle);

    $('dt', srchcont).click(function () {
        if ($(this).hasClass(clsAct)) {
    	 	$(this).removeClass(clsAct).next().slideToggle('fast');
            active = null;
        } else {
            if (isSingle) {
        	   $(active).removeClass(clsAct).next().slideToggle('fast');
            }
			$(this).addClass(clsAct).next().slideToggle('fast');
			active = this;
		}
    });

    $('input#buttonSearch').bind('click', function () {
        var ageValF = $('input#agefrom').val();
        var ageValT = $('input#ageto').val();
        var typeOfV = readCookie('typeOfView');
        if (ageValF === '' && ageValT !== ''){
            $('input#agefrom').val('18');
        }
        $('input#typeOfView').val(typeOfV);
    });

    $('#header #login p a#forgot-password,#homepage #home-tabs #hmember-login a#forgot-password').easyBox({
        target:'#recover-password',
        title:'Recover Password'
    });
});

function changePage(pageNo) {
    $('#othercams .pageID').val(pageNo);
    $('#othercams .search-pagination').submit();
    return false;
}

function viewType(viewNo) {
    $('#othercams .typeOfView').val(viewNo);
    createCookie('typeOfView',viewNo,365);
    $('#othercams .search-pagination').submit();
    return false;
}

function orderBy(ordNo) {
    $('#othercams .orderBy').val(ordNo);
    $('#othercams .search-pagination').submit();
    return false;
}

function emo(tName, cName) {
    var tags, icop, tlen, i;
    tags = document.getElementsByTagName(tName);
    icop = '/img/icons/emoticons/';
    tlen = tags.length;
    if (typeof cName !== "undefined" && cName !== null) {
        for (i = 0; i < tlen; i++) {
            if (tags[i].className.match(cName)) {
                display.changeSmiles(tags[i], icop);
            }
        }
    } else {
        for (i = 0; i < tlen; i++) {
            display.changeSmiles(tags[i], icop);
        }
    }
}

function favoriteAdd(link) {
    var reqURL, reqParam, callback;
    reqURL   = link.href;
    reqParam = {ajaxReqType: true};
    callback = function (data) {
        if (data.add) {
            $(link).parent().children('a.add').hide().end()
                .children('a.remove').show().end()
                .addClass('remove');
        } else if (data.remove) {
            $(link).parent()
                .children('a.remove').hide().end()
                .children('a.add').show().end()
                .removeClass('remove');
        } else {
            alert('Error. Please, try later...');
            return;
        }
    };
    $.post(reqURL, reqParam, callback, 'json');
    return false;
}

function launchWindow() {
    $.easyBox({
        target: '#logon-error',
        title:  'Login Error'
    });
}

function toggleLastOnline(){
    var f = document.getElementById('formSearch');
    if (f['s[isLive]'].checked){
        f['s[daysAgo]'].value = '';
    }
    f['s[daysAgo]'].disabled = f['s[isLive]'].checked;
}

var messages = {"notanumber": "Not a number!", "invalidYear": "Invalid year!", "invalidMonth": "Invalid month!", "invalidDay": "Invalid day!"};

function checkFields(){
    var f = document.getElementById('formSearch');
    if (f['s[ageFrom]'].value !== '' && isNaN(f['s[ageFrom]'].value)){
        f['s[ageFrom]'].select();
        f['s[ageFrom]'].focus();
        alert(messages.notanumber);
        return false;
    }
    if (f['s[ageTo]'].value !== '' && (isNaN(f['s[ageTo]'].value))){
        f['s[ageTo]'].select();
        f['s[ageTo]'].focus();
        alert(messages.notanumber);
        return false;
    }
    if (f['s[daysAgo]'].value !== '' && (isNaN(f['s[daysAgo]'].value))){
        f['s[daysAgo]'].select();
        f['s[daysAgo]'].focus();
        alert(messages.notanumber);
        return false;
    }
    return true;
}
