function run_slideshow(t) {
  if( undefined === t ) t = 0;
  for( var i = 0; i < 4; i++ ) {
    $('#header-image-'+i+' img').each( function(i, item) {
     $(item).remove();
    });
  }
  $.getJSON('/header_images.php', {main_section: main_section, default_images: default_images, iteration: t}, function(data) {
    header_image_data = data;
    setup_data(t, data);
  });
}
function setup_data(t, data) {
    var slots = [];
    for( var i = 0; i < data.length; i++ ) {
      if( null == slots[i%4] ) slots[i%4] = [];
      slots[i%4].push(data[i]);
    }
    var num_full_sets = Math.floor(data.length/4);
    for(var n = 0; n < num_full_sets; n++) {
      var full_set = [];
      for( var x = 0; x < 4; x++ ) {
        full_set[x] = slots[x][n];
      }
      add_set(full_set, n);
    }
    t++;
    cycle_all(t);
}

function randOrd(){
return (Math.round(Math.random())-0.5); } 

function reshuffle_restart(t) {
  for( var i = 0; i < 4; i++ ) {
    $('#header-image-'+i+' img:hidden').each( function(i, item) {
      //if( $('#header-image-'+i+' img:visible').length > 1 ) {
        $(item).remove();
      //}
    });
  }
  header_image_data.sort( randOrd  );  
  setup_data(t, header_image_data);
}

function cycle_all(num_loops) {
  for( var i = 0; i < 4; i++ ) {
  $('#header-image-'+i).cycle({ 
    fx:     'fade', 
    speed:  400 + (i * 400),  //1000 + (i * 1000)
    timeout: 4000, //10000,
    nowrap: 0,
    autostop: 0,
    curr: i//,
    //end: function(options) { reshuffle_restart(num_loops); }
  });
  }
}

function add_set(full_set, set_num) {
  default_images = full_set;
  for(var i = 0; i < full_set.length; i++) {
    $("#header-image-"+i).append("<img src=\"" + full_set[i] + "\" width=\"135\" height=\"135\"/>");
  }
}

function run_header_project_animation() {
  //$("#header-images").fadeTo(1500, 0.1);
  //$("#header-images").after('<img id="header-slogan" style="position:relative; display:none;" src="/images/header-projects/slogan.gif"/>');  
  $("#header-slogan").css( { 'left': '300px', 'top': '82px' });
  $("#header-slogan").show();
  //$("#header-slogan").animate({ 'opacity': 1, 'left': '237px' }, 2000);
  $("#header-slogan").animate({ 'opacity': 1, 'left': '232px' }, 2000);
}
function sortcustom(data_A, data_B)
{
    return (data_A - data_B);
}
$(document).ready(function() {
    if(main_section == 'home'){ 
        t = 0;    
        $.getJSON('/home_images.php', {main_section: main_section, default_images: default_images, iteration: t}, function(data1) {
            var data = new Array();
            data = data1.sort();
            for( var i = 0; i < data.length; i++ ) {
                var style = 'style="display:none;width:inherit;height:inherit;"';
                if(i==0) style = 'style="display:inline;"';
                $('#image-container div#center_img').append('<img '+style+' src="'+data[i]+'">');
            }
            var count = 0;
            var count_j = 0;
            /*$('#image-container div#center_img').each(function(idx,domEle){
                $(domEle).find('img').css('margin',(count_j*149*(-1))+'px 0px 0px '+(count*149*(-1))+'px');
                count++;
                if(count==4) count_j++;
                if(count==4) count = 0;
            });*/
            /*$('#image-container div#center_img').each(function(idx,domEle){
                    $(domEle).cycle({ 
                    fx:     'fade', 
                    speed:  400 + (idx),
                    timeout: 4000 + (idx), 
                    nowrap: 0,
                    autostop: 0,
                    curr: idx  
                });
            });*/
            $('#image-container div#center_img').cycle({ 
                    fx:     'fade', 
                    speed:  1900,
                    timeout: 5000, 
                    nowrap: 0,
                    autostop: 0
            });
        });
        run_header_project_animation();  
    }
    else if( main_section != 'projects') {
        run_slideshow();
    }   
    else {
        run_header_project_animation();
    }
});

