var Rotator;

$(document).ready(function() {

    Rotator = {
        el: '',
        id: 1,
        main: '',
        max: '',
        delay: 360,
        rel: '',
        thumb_url: '',
        loader: new Image(),

        prepare: function() {
            Rotator.el = $(this);
            if (Rotator.el.data('timeout')) return false;

            Rotator.rel = Rotator.el.attr('rel').split(",");
            Rotator.main = Rotator.rel[0];
            Rotator.max = Rotator.rel[1];
            Rotator.thumb_url = Rotator.el.attr('src');
			if(Rotator.max>1)
            	Rotator.swap();
        },

        start: function() {
            try {
                Rotator.loader.src = Rotator.thumb_url.replace(Rotator.main + "s.jpg", Rotator.id + "s.jpg");
                Rotator.loader.onload = Rotator.swap;
                Rotator.el.attr("src", Rotator.loader.src);
                if (Rotator.loader.loaded)
                Rotator.swap();
            } catch(e) {}
        },
        swap: function() {
            try {

                if (Rotator.id > (Rotator.max - 1))
                Rotator.id = 1;
                window.clearTimeout(Rotator.el.data('timeout'));
                //window.setInterval(Rotator.start(), Rotator.delay);
                Rotator.el.data('timeout', window.setTimeout(Rotator.start, Rotator.delay));
                Rotator.id++;
            } catch(e) {}

        },

        stop: function() {
            try {

                window.clearTimeout(Rotator.el.data('timeout'));
                Rotator.el.removeData('timeout');
                Rotator.el.attr("src", Rotator.thumb_url);
                Rotator.id = 1;
                Rotator.el = false;

            } catch(e) {}

        }
    };

    $(".rotation").mouseenter(Rotator.prepare);
    $(".rotation").mouseleave(Rotator.stop);

});


function addRating(id, rating){

    $.ajax({
        type: "POST",
        url: '/ajax.php',
        data: "id=" + id + "&rating=" + rating + "&mode=rating",

        beforeSend: function() {
            return;
        },
        success: function(data) {

            var ResObj = new Object();
            ResObj = jQuery.parseJSON(data);

            $(".graph1").css("width", ResObj.rating_good_per + "%");
            $(".graph2").css("width", ResObj.rating_bad_per + "%");
        },
        error: function() {
            return;
        }

    });
}


function relatedVideo(id, video_id) {
    $.ajax({
        type: "POST",
        url: '/ajax.php',
        data: "video=" + video_id + "&mode=ralated_video",

        beforeSend: function() {
            $("#" + id).html = "LOADING...";
            return;
        },
        success: function(data) {

            var ResObj = new Object();
            ResObj = jQuery.parseJSON(data);
            $("#" + id).html = ResObj.result;
            return;
        },
        error: function() {
            $("#" + id).html = "ERROR";
            return;
        }

    });

}


function el(id) {
    return document.getElementById(id)
}

function show(id) {
    return $('#' + id).slideDown();
}

function hide(id) {
    return $('#' + id).fadeOut();
}

function show_hide(id) {
    if ($('#' + id).css('display') == 'none')
    return $('#' + id).slideDown();

    return $('#' + id).fadeOut();
}

function redirect(url) {
    window.location = url;
    return false;
}

function if_confirm(url) {
    if (confirm(text_are_you_sure))
    redirect(url);
}

function Submit(form) {
    f = document.forms[form];
    f.submit();
}

function Bookmark(title, url) {
    if (window.sidebar) {
        window.sidebar.addPanel(title, url, "");
    } else if (window.external) {
        window.external.AddFavorite(url, title);
    } else if (window.opera && window.print) {
        var a = document.createElement('A');
        if (!a) return false;
        //IF Opera 6
        a.setAttribute('rel', 'sidebar');
        a.setAttribute('href', url);
        a.setAttribute('title', title);
        a.click();
    }
}

function charcount(name, max) {
    memo = el('_form_' + name);
    counter = el(name + "__charcounter");

    msglen = memo.value.length;
    if (msglen > max) memo.value = memo.value.substring(0, max);
    counter.value = memo.value.length;
}

function clearmemo(name) {
    memo = el('_form_' + name);
    memo.value = '';
    counter = el(name + "__charcounter");
    counter.value = 0;
}

function SendGet(url) {
    redirect(url);
}

function show_message(message) {
    var scroll = getBodyScrollTop();
    sc = 50;
    sc = scroll;
    $("#message_block").css("right", "25px");
    $("#message_block").css("top", 25 + sc + "px");
    $("#message_block").css("width", "210px");
    $("#message_block").css("height", "40px");

    $("#message_block").empty();
    $("#message_block").append(message);
    $("#message_block").show();
    setTimeout("$('#message_block').fadeOut('slow')", 2000);
}

function getClientCenterX() {
    return parseInt(getClientWidth() / 2) + getBodyScrollLeft();
}

function getClientCenterY() {
    return parseInt(getClientHeight() / 2) + getBodyScrollTop();
}

function getBodyScrollTop() {
    return self.pageYOffset ||
    (document.documentElement && document.documentElement.scrollTop) ||
    (document.body && document.body.scrollTop);
}

function getBodyScrollLeft() {
    return self.pageXOffset ||
    (document.documentElement && document.documentElement.scrollLeft) ||
    (document.body && document.body.scrollLeft);
}

function getClientWidth() {
    return document.compatMode == 'CSS1Compat' && !window.opera ? document.documentElement.clientWidth: document.body.clientWidth;
}

function getClientHeight() {
    return document.compatMode == 'CSS1Compat' && !window.opera ? document.documentElement.clientHeight: document.body.clientHeight;
}

function popup(link, width, height, resizable, scroll) {
    if (!window.focus)
    return true;

    var href;
    if (typeof(link) == 'string')
    href = link;
    else
    href = link.href;
    if (!resizable)
    resizable = 'yes';

    if (!scroll)
    scroll = 'no';

    window.open(href, '', 'width=' + width + ',height=' + height + ',scrollbars=' + scroll + ',resizable=' + resizable + ',alwaysRaised=yes');
}

function Block(html, width, height) {

    $.blockUI(
    {
        message: html,
        css: {
            top: '20%',
            padding: '15px',
            backgroundColor: '#333333',
            border: '1px solid #111111',
            color: '#f0ffff',
            cursor: 'default'
        }
    }
    );
    $('.blockOverlay').attr('title', 'Click to unblock').click($.unblockUI);
}

function selectfield(id) {
    show(id);
}

function Locale(lang) {
    switch (lang) {
    case 'en':
        setCookie("language", "en", 30, "/", "." + SITE_DOMAIN);
        break;
    case 'ja':
        setCookie("language", "ja", 30, "/", "." + SITE_DOMAIN);
        break;
    default:
        setCookie("language", "en", 30, "/", "." + SITE_DOMAIN);
        break;
    }
    window.location.reload();
}

function setCookie(name, value, expires, path, domain, secure) {
    var expdate = new Date();
    document.cookie = name + "=" + escape(value) +
    ((expires) ? "; expires=" + (expdate.getDate() + expires) : "") +
    ((path) ? "; path=" + path: "") +
    ((domain) ? "; domain=" + domain: "") +
    ((secure) ? "; secure": "");
}

function getCookie(c_name)
 {
    if (document.cookie.length > 0)
    {
        c_start = document.cookie.indexOf(c_name + "=");
        if (c_start != -1)
        {
            c_start = c_start + c_name.length + 1;
            c_end = document.cookie.indexOf(";", c_start);
            if (c_end == -1) c_end = document.cookie.length;
            return unescape(document.cookie.substring(c_start, c_end));
        }
    }
    return "";
}



function CheckEmail() {
    formid = "_form_email";
    helpid = "_help_email";
    e = el(formid);
    if (e.value.length > 3) hide(helpid);
}

function CheckEmail_ajax(name) {
    formid = "_form_email";
    helpid = "_help_email";
    e = el(formid);
    $.post('ajax.php', {
        mode: "check_user_email",
        email: e.value
    },
    CheckEmail_result);
}

function CheckEmail_result(result) {
    formid = "_form_email";
    helpid = "_help_email";

    if (result == 'ok') return hide(helpid);

    show(helpid);
    el(helpid).innerHTML = result;
    el(helpid).className = "error_field";
}

function CheckUser() {
    formid = "_form_name";
    helpid = "_help_name";
    e = el(formid);
    if (e.value.length > 3) hide(helpid);
}

function CheckUser_ajax(name) {
    formid = "_form_name";
    helpid = "_help_name";
    e = el(formid);
    return $.post('ajax.php', {
        mode: "check_user_name",
        name: e.value
    },
    CheckUser_result);
}

function CheckUser_result(result) {
    formid = "_form_name";
    helpid = "_help_name";

    if (result == 'ok') return hide(helpid);

    show(helpid);
    el(helpid).innerHTML = result;
    el(helpid).className = "error_field";

    return true;
}


function ShowRecip(id)
 {

    $(".show_recip").each(function() {
        $(this).hide();
    });
    $("#recip_fields >p").hide();
    if (id != 0)
    $("#recip_" + id).show();
    else
    {
        $("#recip_fields >p").show();
    }

    return;
}

