var doingSearch = false;

var CurrentURL = location.href;

var stLocation = CurrentURL.indexOf('search_type=') + 12;

var urlEnd = CurrentURL.length;



for (var x=stLocation; x<urlEnd; x++) {
        if (CurrentURL.charAt(x) == "&") { var urlEnd = x; break;}

       }



	if (stLocation != 11) {

            var        searchType = CurrentURL.slice(stLocation,urlEnd);

	}

	else {

            var searchType = 'buscar';

	}

	 if ((searchType == 'uniclave') && (CurrentURL.indexOf('buscar_home') == -1)) {

	                        searchType = 'buscar';

	            }



function doBodySearch(st, sf) {

	if( doingSearch )
	{
		alert( "Tu búsqueda esta en proceso. Refresca la página para entrar otra búsqueda." );
		return false;
	}

var query = document.getElementById(sf).query.value;

var is_clicked_link = document.getElementById(sf).is_clicked_link.value;
var is_clicked_param='';
if(is_clicked_link != null && is_clicked_link!='')
    is_clicked_param = '&is_clicked_link=yes';

if (st == 'button') { st = searchType; }



if (query == '') {

            //location.href = '/buscar/buscar_home.jhtml?search_type=' + st;

}

else {

		if (st == 'web_video') {
		location.href = '/video/buscar.jhtml?query=' + encodeURIComponent(query) + is_clicked_param;
		}
		else if (st == 'site') {
		location.href = '/buscar/buscar_resultados.jhtml?base=0&locale=0&pgsz=10&schid=12&type=basic&search_type=site&query=' + encodeURIComponent(query) + is_clicked_param;
		}
		else if (st == 'compras') {
		location.href = 'http://comparaprecios.thegoldmusic.com/search.php?Search=Buscar&topcat_id=&form_keyword=' + encodeURIComponent(query);
		}
		else if (st == 'uniclave2') {
		location.href = '/buscar/buscar_resultados.jhtml?base=0&locale=0&pgsz=10&schid=12&type=basic&search_type=uniclave&query=' + encodeURIComponent(query)+ is_clicked_param;
		}
		else if (st == 'uniclave') {
		location.href = '/buscar/buscar_resultados.jhtml?base=0&locale=0&pgsz=10&schid=12&type=basic&search_type=internet&query=' + encodeURIComponent(query)+ is_clicked_param;
		}

		else if (st == 'english') {
		location.href = '/buscar/buscar_resultados.jhtml?base=0&locale=2&pgsz=10&schid=12&type=basic&search_type=internet&query=' + encodeURIComponent(query)+ is_clicked_param;
		}
		else if (st == 'foros') {
		location.href = '/buscar/buscar_resultados.jhtml?base=0&locale=0&pgsz=10&schid=12&type=basic&search_type=foros&query=' + encodeURIComponent(query) + '&gooch=foros&goosite=http://foro.thegoldmusic.com'+ is_clicked_param;

        }
        else if (st == 'video') {
		location.href = '/buscar/buscar_resultados.jhtml?base=0&locale=0&pgsz=10&chid=19641&type=basic&search_type=video&query=' + encodeURIComponent(query)+ is_clicked_param;
		}
		else {
		location.href = '/buscar/buscar_resultados.jhtml?base=0&locale=0&pgsz=10&schid=12&type=basic&search_type=internet&query=' + encodeURIComponent(query)+ is_clicked_param;
		}

		doingSearch = true;
		return false;
   }

}


function doSearch(oForm, strType, strBase, strCategory, strLocale, strPgsz, strQuery)
{
       

	if( doingSearch )
	{
		alert( "Tu búsqueda esta en proceso. Refresca la página para entrar otra búsqueda." );
		return false;
	} // endif

	if (strQuery != null)
	{
		// Set query to bypass empty check
		oForm.query.value = strQuery;
		
	}

	if( ( oForm.query.value == null || oForm.query.value == "" ) && strType != "buscar" && strType!="web_video" )
	{
		if( strType != null )
		{
			if (strType == "uniclave")
				alert( "Nuestras UniClaves te dan la oportunidad de encontrar una página en thegoldmusic.com o en otros sitios recomendados. Por favor, dinos qué buscas." );
		}
		else
		{
			location.href = "http://www.thegoldmusic.com/rdt.dyn?trt=31&rid=4000&requrl=http://www.thegoldmusic.com/content/channel.jhtml?chid=1&schid=12";
		}

		oForm.query.focus();
	}
	else
	{
		if (strType != null)
		{
			oForm.search_type.value = strType;
		}
		if (strBase != null)
		{
			oForm.base.value = strBase;
		}
		if (strCategory != null)
		{
			oForm.category.value = strCategory;
		}
		if (strLocale != null)
		{
			oForm.locale.value = strLocale;
		}
		if (strPgsz != null)
		{
			oForm.pgsz.value = strPgsz;
		}

		if (strQuery != null && strCategory != null)
		{
			// Unset 'query' parameter
			oForm.query.value = "";
		}
		doingSearch = true;

		
		for(var i=0;i<oForm.search_type.length;i++)
		{

		 if(oForm.search_type[i].checked && oForm.search_type[i].value == "compras")
		 {
		  	oForm.action = "http://comparaprecios.thegoldmusic.com/search.php?Search=Buscar&topcat_id=";

			oForm.query.name = 'form_keyword';

		 }		 
		 if(oForm.search_type[i].checked && oForm.search_type[i].value == "web_video")
		 {
		  	oForm.action = "/video/buscar.jhtml";

		 }
		 if(oForm.search_type[i].checked && oForm.search_type[i].value == "video")
		 {
		   oForm.chid.value='19641';
		   oForm.schid.value='0';
		   oForm.display.value='portal';
		 		  	
		 }

		}
		
		
		if(oForm.search_type.value == "web_video")
		{		   
		   oForm.action = "/video/buscar.jhtml";
		}
		if(oForm.search_type.value == "video")
		{		   
		  oForm.chid.value='19641';
		  oForm.schid.value='0';
		  oForm.display.value='portal';
		}


		oForm.submit();
	}

	return false;
}

function setLocale(oForm, strLocale)
{
	oForm.locale.value = strLocale;



}

function setLetter(oForm, strLetter)
{
	oForm.letter.value = strLetter;
}

function searchFocus(oForm)
{
	oForm.query.focus();
}

function openVideoLink( videoid )
{
    if (schid == null || schid != 11529)
    {
        location.href = '/content/videoplayer.jhtml?cid=' + videoid;
    }
    else
    {
        var child;
        if (child != undefined) {child.close; } 

        var left = 3; 
        var top = 3;
        var width = 750;
        var height = 640;

        var ourl = '/content/video.jhtml?cid=' + videoid;

        if ( navigator.appName.indexOf('Microsoft') != -1 )
        {
            left = window.screenLeft + ( window.document.body.clientWidth/2 - width/2 );
            top =  window.screenTop;
        }
        else
        {
            left = window.screenX + ( window.outerWidth/2 - width/2 );
            top =  0;
        } /* endif */

        child = window.open(ourl,'videopopup','left=' + left + ',top=' + top + ',screenx=' + left + ',screeny=' + top + ',scrollbars=no,width=' + width + ',height=' + height);
    }
}


jQuery(document).ready(function(){

	//jQuery.noConflict();

//UNDERLINE TEXT WHEN ROLLING OVER AN IMAGE.-------------------------------------------------
	jQuery(".hl_text").each(function(){


		 var underlineLink =  jQuery(this).parent().find("a, a span").not(jQuery(this));

		 jQuery(this).hover(
			  function (){
				 //alert(underlineLink);
				underlineLink.addClass("underlineText");
			  },
			  function (){
				underlineLink.removeClass("underlineText");
			  });
	});
	
    
        
    
    jQuery('#cdtrigger a').click( function() {
        if(jQuery('#cdcontent').is(":hidden")) {
              jQuery('#cdcontent').slideDown('normal');           
             jQuery('#cdtrigger').removeClass("closed").addClass("opened");
        } else {
            jQuery('#cdcontent').slideUp('normal', function(){
                jQuery('#cdtrigger').removeClass("opened").addClass("closed");
            });
        }    
        return false;
    });

    jQuery('#category_dropdown').hover(
        function() {
            if(typeof tmr != "undefined") {
                clearTimeout(tmr);
            }
          },
        function() {
            tmr = setTimeout( "if(jQuery('#cdcontent').is(':visible')) jQuery('#cdcontent').slideUp('normal', function(){jQuery('#cdtrigger').removeClass('opened').addClass('closed');});" , 2000);
        }
    );

    jQuery('.dropdown_close a').click( function() {
        jQuery('#cdcontent').slideUp('normal', function() {
            jQuery('#cdtrigger').removeClass("opened").addClass("closed");
        });    
        return false;
    });

    
    
});








// SEARCH TOGGLE

(function($){

    $.fn.searchWidget2 = function(params){

        var params = $.extend({

            wrapperId: 'search',

            defaultText: 'Escribe una frase o Uniclave aquí',

            hotLinks: []

        }, params);



        var jTabs = $('#'+params.wrapperId+' .search_tabs li');

        var jForm = $('#'+params.wrapperId+' form');

        var jSearchInput = $('#'+params.wrapperId+' .search_input_val');

        var jSubmit = $('#'+params.wrapperId+' .search_input_btn');

        var jLinks = $('#'+params.wrapperId+' .search_links ul li');

        


        // reset the form

       // jForm[0].reset();



        // tabs behavior

        jTabs.each(function(){



            $(this).click(function(){

                jTabs.each(function(){

                    $(this).removeClass("active");

                });

                $(this).addClass("active");



                // select radio

                $(this).find('input').attr('checked', true);



                // Submit form if something in text field

                if( ( jQuery.trim( jSearchInput.attr('value') ) != params.defaultText )

                    && ( jQuery.trim( jSearchInput.attr('value') ) != '' ) ) {

                    execSubmit();

                }

                return false;

            });

        });





        // Popular links behavior
	jLinks.each(function(){
	        $(this).click(function(){
	        $(jSearchInput).attr( 'value', $(this).find('a').attr('alt') );
            
            jForm.find('#is_clicked_link').val('yes');
            
	         execSubmit();
	            });
	        });






        $(jSubmit).click(function () {
            execSubmit();
           return false;

        });



         function execSubmit() {
            if( jForm.find('#search_type_compras').attr('checked') ) {
                location.href = 'http://comparaprecios.thegoldmusic.com/search.php?Search=Buscar&topcat_id=&form_keyword=' + encodeURIComponent( jSearchInput.attr('value') );
                return false;
            }

            doBodySearch($("form#" +jForm.attr('id')+ " input[@name='search_type']:checked").val(), jForm.attr('id'));
            return false;
        }




    };

})(jQuery);



function writePopularSearchLinks( randArLinks ) {
    if( randArLinks.length > 0 ) {
        var arLinkObjs = randArLinks[Math.floor( Math.random() * randArLinks.length )];
        for( var i=0; i<arLinkObjs.length; i++ )
            document.write('<li'+ ((i==0)? ' class="f"' : '') +'><a href="javascript:void(0);" alt="'+ arLinkObjs[i].searchStr +'">'+ arLinkObjs[i].dispName +'</a></li>');
    }
}

