var timeout    = 500;
var closetimer = 0;
var ddmenuitem = 0;

var s = null;
$(function(){
    // slideshow
    /*
    $.getJSON("slideshow.php", { category: $('#category').val() }, function(json) { 
        s = new SlideShow(json); 
        $("#slideshow").show();

        // play slideshow
        s.start('s');
    });
    */
    
    // gallery
    /*
    $('img.image1').data('ad-desc', 'Whoa! This description is set through elm.data("ad-desc") instead of using the longdesc attribute.<br>And it contains <strong>H</strong>ow <strong>T</strong>o <strong>M</strong>eet <strong>L</strong>adies... <em>What?</em> That aint what HTML stands for? Man...');
    $('img.image1').data('ad-title', 'Title through $.data');
    $('img.image4').data('ad-desc', 'This image is wider than the wrapper, so it has been scaled down');
    $('img.image5').data('ad-desc', 'This image is higher than the wrapper, so it has been scaled down');
    */

	$('#dropdown > li').bind('mouseover', dropdown_open);
	$('#dropdown > li').bind('mouseout',  dropdown_timer);
	document.onclick = dropdown_close;



    var galleries = $('.ad-gallery').adGallery();
    $('#switch-effect').change(
      function() {
        galleries[0].settings.effect = $(this).val();
        return false;
      }
    );
    $('#toggle-slideshow').click(
      function() {
        galleries[0].slideshow.toggle();
        return false;
      }
    );
    $('#toggle-description').click(
      function() {
        if(!galleries[0].settings.description_wrapper) {
          galleries[0].settings.description_wrapper = $('#descriptions');
        } else {
          galleries[0].settings.description_wrapper = false;
        }
        return false;
      }
    );
});

// make a resquest without args
function tabchange(page) {
    //s.stop();
    // if exists, remove
    $('#LinkForm').remove();
    $.ajax({url:page,type:"post",data:"",success:function(data){$("#main").html(data);}})
}

// make a request with an argument
function myLink(args) {
    // if exists, remove
    $('#LinkForm').remove();

    // create a hidden div
    var $hiddenDiv = $('<div style="hidden" />');

    /// create the form
    var $form = $('<form name="LinkForm" id="LinkForm" method="POST" action="" />');
    var $submitData = $('<input type="hidden" name="category" id="category" value="'+args+'"/>');
    $($form).append($submitData);
    $($hiddenDiv).append($form);
    $('body').append($hiddenDiv);
    $('#LinkForm').submit();
    teste();
}

// slideshow
function SlideShow(files)
{
    this._files = files;
    //console.debug(this._files);
    this._current_index = -1;
    this._interval = null;

    this.next = function() {   
        this._current_index += 1;
        if (this._current_index > (this._files.length - 1))
            this._current_index = 0;

        //hide img
        $("#slideimg").remove();

        var obj_img = $("<img id='slideimg' />");
        
        //change img attributes
        $(obj_img).attr('height', this._files[this._current_index]['height']);
        $(obj_img).attr('width', this._files[this._current_index]['width']);
        $(obj_img).attr('src', this._files[this._current_index]['file']);
        $(obj_img).attr('style', 'opacity: 1');
        $(obj_img).attr('class', '');
        
        //show img
        $("#main_content").html(obj_img);
    }

    this.start = function(object_name)
    {
        this.next();
        this._interval = window.setInterval(object_name + ".next();", 3500);
    }

    this.stop = function() {
        if (this._interval != null)
            window.clearInterval(this._interval);
    }
}

function StopSlideShow()
{
    s.stop();
}

function StartSlideShow()
{
    s.start('s');
}

function dropdown_open()
{  dropdown_canceltimer();
   dropdown_close();
   ddmenuitem = $(this).find('ul').css('visibility', 'visible');
}

function dropdown_close()
{  if(ddmenuitem) ddmenuitem.css('visibility', 'hidden');}

function dropdown_timer()
{  closetimer = window.setTimeout(dropdown_close, timeout);}

function dropdown_canceltimer()
{  if(closetimer)
   {  window.clearTimeout(closetimer);
      closetimer = null;}
}


