﻿$(document).ready(function() {

    var _q = getParam("q");
    //alert(_q);
    if (_q != "") {
        $('.busca').val(_q);
    }

    $('.busca').click(function() {
        resetSearch($(this).val());
    });
    function resetSearch(_ref) {
        //debugger;
        if (_ref == "搜索") {

            $('.busca').val("");
        }
    }

    $('.busca').blur(function() {
        if ($(this).val() == "") {
            $(this).val("搜索");
        }
    });

    $('.send').click(function() {
        //debugger;
        var _keyword = $('.busca').val();
        if (_keyword == "" || _keyword == "搜索") {
            return false
        } else {

            var _url = "/search.aspx?q=" + _keyword;
            //window.location.href = _url;
            window.open(_url);
        }
    });

    function getParam(paramName) {
        paramValue = "";
        isFound = false;
        if (this.location.search.indexOf("?") == 0 && this.location.search.indexOf("=") > 1) {
            arrSource = unescape(this.location.search).substring(1, this.location.search.length).split("&");
            i = 0;
            while (i < arrSource.length && !isFound) {
                if (arrSource[i].indexOf("=") > 0) {
                    if (arrSource[i].split("=")[0].toLowerCase() == paramName.toLowerCase()) {
                        paramValue = arrSource[i].split("=")[1];
                        isFound = true;
                    }
                }
                i++;
            }
        }
        return paramValue;
    }
    /*Tras. Todos Select*/
    /*$('select').each(function(index) {
    $(this).sexyCombo({
    autoFill: false
    });	

			/* COMBOBOX
    _a = $(this).parent().find('li').size();
    _a = _a - 1;
			
    $(this).parent().find('li').each(function(index) {			
				
    if(index == _a){
    $(this).addClass("last");
						
    }
    });
    });*/
    /*FIM Tras. Todos Select*/

    /* Tras. Todos a em Player*/
    /*
    Exemplo:
    <a href="http://www.youtube.com/watch?v=sxUjB3Q04rQ" class="player" rel="150|100">Video</a>
    */
    aToVideo();

    /* Fim Tras. Todos a em Player*/
    /* Tras. Todos a externo em _blank*/
    $("a[href^='http:']:not([href*='http://" + window.location.host + "'][target='_blank'])").live('click', function() {
        $(this).attr('target', '_blank');
    });
    /* Fim Tras. Todos a externo em _blank*/




    $('.language li:first-child').show();
    $('.language li:first-child').addClass('active');
    activeSelectLanguage();

    _statusLanguage = false;
    _selectedLanguage = 0;
    timers = new Array();
    function activeSelectLanguage() {
        $('.language ul li').each(function(index) {
            $(this).click(function() {
                if ($(this).attr('class')) {
                    if (!_statusLanguage) {
                        refreshLanguage(index);
                        //$(this).delay(3000, hideLanguageAutomatic );
                        //window.setTimeout = function(code, interval) {
                        timers.push(setTimeout(hideLanguageAutomatic, 3000));
                        //}
                        //
                        _statusLanguage = true;
                    } else {
                        clearTimeouts();
                        refreshLanguageHide(index);
                    }
                } else {
                    clearTimeouts();
                    refreshLanguageHide(index);
                }
            });
        });
    }

    function refreshLanguage(_idx) {
        $('.language ul li').each(function(index) {
            $(this).show();
        });
    }

    function refreshLanguageHide(_idx) {
        $('.language ul li').each(function(index) {
            if (index == _idx) {
                $(this).show();
                _selectedLanguage = index;
                $(this).addClass('active');
            } else {
                $(this).hide();
                $(this).removeClass('active');
            }
        });
        _statusLanguage = false;
    }

    function hideLanguageAutomatic() {
        if (_statusLanguage) {
            clearTimeouts();
            //refreshLanguageHide(_selectedLanguage);
        }
    }

    function clearTimeouts() {
        for (var i = 0; i < timers.length; i++) {
            clearTimeout(timers[i]);
        }
        resetTimeouts();
    }

    function resetTimeouts() {
        timers = new Array();
        refreshLanguageHide(_selectedLanguage);
    }



    // menu principal
    $('#header ul.navigation > li > a, #header ul.navigation > li > ul > li > a').mousedown(function() {
        $(this).parent().addClass('active');
    }).mouseup(function() {
        $(this).parent().removeClass('active');
    });

    /*
    function resetNavigation(_idx){
    $('#header ul.navigation li').each(function(index) {
    if(index != _idx){
    $(this).removeClass('active');
    }
    });
    }

        function resetNavigationAll(){
    $('#header ul.navigation li').each(function(index) {
    $(this).removeClass('active');
    });
    }
    */

});

function aToVideo(){
    $('a.player').each(function(index) {
    _rel = $(this).attr("rel").split("|");
    if(_rel != "undefined" && _rel != ""){
    
        _w = _rel[0];
        _h = _rel[1];
    
    }else{
        _w = 720;
        _h = 480;
    }
    $(this).youtubin({
        swfWidth : _w,
        swfHeight : _h
    });
 });
}
		
