﻿/// <reference path="../../../common/scripts/jquery.scms.js"/>
/// <reference path="../../../common/scripts/jquery.cycle.js"/>
/// <reference path="../../../common/scripts/jquery.scroller.js"/>
/// <reference path="../../../common/scripts/jquery.form.js"/>
/// <reference path="../../../common/scripts/swfobject.js"/>

$(document).ready(function()
{
    var spacer = rootPath + 'common/images/spacer.gif';

    $('div.section-menu a').dropdown();

    $('#tabs').tabs();

    $('#slide-show .wrapper').cycle({
        fx: 'fade',
        timeout: 3000,
        delay: -1000,
        speed: 1000,
        after: null,
        pause: true,
        sync: true,
        next: '#slide-show .controls .next',
        prev: '#slide-show .controls .previous'
    });

    $('#sponsor-slides').cycle({
        fx: 'turnDown',
        timeout: 5000,
        delay: -1000,
        speed: 1000,
        after: null,
        pause: true,
        sync: true
    });

    $('.news-marquee .marquee-inner').marquee();

    $('.resize-images img').each(function()
    {
        var width = $(this).width();
        if (width > 460)
        {
            $(this).width(460).height('auto');
            $(this).before('<a class="resized-image" href="' + this.src + '" style="width: ' + $(this).width() + 'px; height: ' + $(this).height() + 'px; position: absolute; background: url(' + spacer + ');"></a>');
        }
    });

    $('.light-box .image, .light-box .resized-image').lightBox({
        imageLoading: rootPath + 'common/images/lightbox-ico-loading.gif',
        imageBtnClose: rootPath + 'common/images/lightbox-btn-close.gif',
        imageBtnPrev: rootPath + 'common/images/lightbox-btn-prev.gif',
        imageBtnNext: rootPath + 'common/images/lightbox-btn-next.gif',
        imageBlank: rootPath + 'common/images/lightbox-blank.gif'
    });

    $('.ad-flash-container').each(function()
    {
        var so = new SWFObject(this.getAttribute('src'), '', this.getAttribute('width'), this.getAttribute('height'), '8', '#ffffff');
        so.addParam('wmode', 'transparent');
        so.write(this.id);
    });
});

function applyFilter(page)
{
    var page = isNaN(page) ? 0 : page;
    var time = $('#filter select[name=time]').val();
    var sort = $('#filter select[name=sort]').val();
    var category = $('#filter select[name=category]').val();
    if (time) time = '/' + time;
    if (category) category = '/' + category;
    sort = '/' + sort;
    page = '/trang-' + page;
    location.href = $('#filter').attr('root') + category + time + sort + page;
    return false;
}

function submitForm(form)
{
    $(form).ajaxSubmit({
        dataType: 'script'
    });
    return false;
}

function moveSlide(prefix, count, offset, klass)
{
    if (!count || !offset) return;
    if (!klass) klass = 'hide';
    var index = -1;
    var slides = [];
    for (var i = 0; i < count; i++)
    {
        var id = prefix + i;
        var slide = document.getElementById(id);
        if (!slide) continue;
        slides.push(slide);
        if (slide.offsetWidth > 0 && slide.offsetHeight > 0) index = i;
        $(slide).addClass(klass);
    }
    if (index == -1)
    {
        $(slides[index]).removeClass(klass);
    }
    else
    {
        index += offset;
        if (index < 0) index = slides.length - 1;
        if (index >= slides.length) index = 0;
        var slide = slides[index];
        if (slide) $(slide).removeClass(klass);
    }
}


function doSearch(form)
{
    if (!form) return false;
    var key = form.elements['key'];
    if (!key || !key.value)
    {
        alert(form.getAttribute('requiredMessage'));
        return false;
    }
    if (key.value.length < 3)
    {
        alert(form.getAttribute('invalidMessage'));
        return false;
    }
    location.href = form.action + '?' + encodeURIComponent(key.value);
    return false;
}