// Shows and Hides top menu
function initMenuTop (self) {
    self.parent().children(':nth-child(2)').hide();
    self.mouseover(function(event){
        $('.headerBottom > ul > li.subMenu > a').removeClass('selected');
        $('.headerBottom > ul > li.subMenu > a').parent().children(':nth-child(2)').hide();

        $(this).addClass('selected');
        $(this).parent().children(':nth-child(2)').show();
		event.stopPropagation();
	});
    $('.headerBottom > ul > li:not(.subMenu), .headerTop').mouseover(function(event) {
        $('.headerBottom > ul > li.subMenu > a').removeClass('selected');
        $('.headerBottom > ul > li.subMenu > a').parent().children(':nth-child(2)').hide();
		event.stopPropagation();
    });
    $('.headerBottom > ul > li.subMenu > ul.subMenu').mouseleave(function(event) {
        $('.headerBottom > ul > li.subMenu > a').removeClass('selected');
        $('.headerBottom > ul > li.subMenu > a').parent().children(':nth-child(2)').hide();
        event.stopPropagation();
    });
    $(document).click(function() {
            self.removeClass('selected');
            self.parent().children(':nth-child(2)').hide();
    });
}

// Slider of Highlights
function initSlider(self) {
	$('.barBlock ul').children().click(function(){self.slider( "value" , $(this).index()+1 );});
}

function thumb (self) {
    self.hover(function () {
                var left = $(this).position().left;
        var ul = self.parent().position().left;
        var dif = ul + left;
        if (dif == 0)
            left = -50;
        else if (dif == 412)
            left = -120;
        else
            left = -80;
        $(this).css({ 'z-index': '10' }); /*Add a higher z-index value so this image stays on top*/
        $(this).find('img').addClass("hover").stop() /* Add class of "hover", then stop animation queue buildup*/
		.animate({
		    marginTop: '-75px', /* The next 4 lines will vertically align this image */
		    marginLeft: left + 'px',
		    top: '50%',
		    left: '50%',
		    width: '162px', /* Set new width */
		    height: '123px' /* Set new height */
		    /*padding: '40px'*/
		}, 200, function () {
		    var me = $(this);
		    $(this).parent().parent().find('.shadow').css({ marginLeft: (44 + left) + 'px' });
		    $(this).parent().parent().find('.shadow').fadeIn("fast");
		}); /* this value of "200" is the speed of how fast/slow this hover animates  'display': 'block', */

    }, function () {
        $(this).parent().parent().find('.shadow').css('display', 'none');
        $(this).css({ 'z-index': '0' }); /* Set z-index back to 0 */
        $(this).find('img').removeClass("hover").stop()  /* Remove the "hover" class , then stop animation queue buildup*/
		.animate({
		    marginTop: '0', /* Set alignment back to default */
		    marginLeft: '0',
		    top: '0',
		    left: '0',
		    width: '88px', /* Set width back to default */
		    height: '69px', /* Set height back to default */
		    padding: '0px'
		}, 400);
    });
}

function HighlightInit(self) {
    lastBlock = $("#a1");
    maxWidth = 280;
    minWidth = 140;

    self.hover(function () {
        $(lastBlock).animate({ width: minWidth + "px" }, { queue: false, duration: 400 });
        $(this).animate({ width: maxWidth + "px" }, { queue: false, duration: 400 });
        lastBlock = this;
    }
        );
}

function slideShowShadow(self) {
    self.children().hover(function () {
        $(this).find('.shadow').slideDown('slow');
    }, function () { $(this).find('.shadow').slideUp('slow'); });
}


//INIT MOUSE OVER CHANGE OPACITY
function initImageOpacityHover(self) {
    self.find('img').animate({ "opacity": "0.2" });
    self.hover(
		function () {
		    $(this).find('img').stop().animate({ "opacity": "1" }, "slow");
		},
		function () {
		    $(this).find('img').stop().animate({ "opacity": "0.2" }, "slow");
		}
	);
}

function initDatePicker(self) {
    /*self.click(function () {
        if ($('#datepick-div').is(':visible'))
            $('#datepick-div').hide();
        else
            $('#datepick-div').show();
    });
    $('#datepick-div').find('.datepick-close').click(function () {
        alert(1);
        $('#datepick-div').hide();
    });*/
}

function initEpisodios(self) {
    self.hover(function () {
        $(this).find('em').show();
    }, function () {
        $(this).find('em').hide();
    });
}


function initSearchOptions (self) {

self.click(function(){

    $.ajax({
            url: 'http://lb1-sun-v4x0.iol.pt:8080/solrfront/publicAdvancedSearch' ,
            data: {
                sort:'',
                sortSize:'',
                data:'2011-01-27',
                ex2:'',
                ex1:'',
                exaux:'',
                dia:'0',
                rows:'10',
                div_id:'5824',
                numRes:'0',
                intrevDatas:'none',
                ex3:'',
                ex4:'',
                q:'cinebox',
                ex5:'',
                tipo:'artigo',
                sortDuracao:'',
                projecto:'TVI Press Room',
                raw:'true'
            },
            success:function(xml){
                xml.find('doc').each(function(){});
            }
        });
    });
}

function searchUrl() {
    var keyword = document.getElementById('searchKeyword').value;
    var startDate = document.getElementById('searchStartDate').value;
    var endDate = document.getElementById('searchEndDate').value;
    var type = document.getElementById('searchType').value;

    if(''==keyword)
        {alert('O texto a pesquisar é de preenchimento obrigatório.')}
    else
    {
        keyword=keyword.replace(/\s/g,'+');
        var requestedQuery = '/pesquisa/'+type+'/'+keyword;
        if(startDate != '' && endDate != '') {
            //requestedQuery += '&amp;dia=['+startDate+'T00:00:00.000Z TO '+endDate+'T23:59:59.999Z]';
            //requestedQuery += '&amp;intrevDatas=['+startDate+'T00:00:00.000Z%20TO%20'+endDate+'T23:59:59.999Z]';
            requestedQuery += '/'+startDate+'/'+endDate;
        }

        window.location = requestedQuery;
    }
}
