/* -- STUDIO -- */

//przechwycenie lacz do galerii i zmiana na onclick
$('body').ready(changeGallery);
$('body').ready(changePano);
//document.onload = changeGallery();

//ie 
try {
      document.execCommand('BackgroundImageCache', false, true);
    } catch(err){}



/** replacing a href's with onclick events */
function changeGallery()
{
//alert('dasda');
  var galleryButtons = $('a.gal_det,a.gal_logo');
   
  galleryButtons.each( function(i)
                  {
                     var id = $(this).attr('id');
                     //var next_indx = id.indexOf('_next'); //id in html is like 'galleryA_XX_nextYY'
                     var gallery_id = id.substring(9);//, next_indx); // only this 'XX'
                     //var next_gallery_id = id.substring(next_indx + 5); // only YY
                     
                     $(this).click(function() {showGallery(gallery_id); return false;});
                  }
  );
}

/** replace a href with onclick event in lab page to show pano */
function changePano()
{
   $('.showPano').each( function()
                   {
                      $(this).click(function() {showPano(); return false;} );
                   }
                   ); 
}

/** main function to show gallery with given id */
function showGallery(id)
{
  var conteiner = $('#extra_content');
  var clientHeight = myClientHeight();
  var clientWidth = myClientWidth();
  
  conteiner.css('height',$('body').height()+'px');//'100%');//clientHeight+'px');
  //conteiner.css('top',window.getScrollY()+'px');
  conteiner.css('width','100%');
  conteiner.css('display','block');
  
  conteiner.append('<div id="studio_bgr"></div>');
  
  var studio_bgr = $('#studio_bgr');
  studio_bgr.css('opacity',0);

	var params = 
	{
		showPrev: true,
		printThumbList: true,
		createBgr: true
	};

  studio_bgr.fadeTo(500,0.9, function() 
                            { 
                              downloadGallery(id,1,params);
                            }
                   );
   
  //conteiner.css('opacity',0);
  //conteiner.fadeTo(500, 0.4);
         
  return false;
}

/** fadeing out gallery panel */
function closeGallery()
{
  var conteiner = $('#extra_content');
  conteiner.fadeOut(500, removeGalleryItems);
}


/** removing dynamicly loaded DOM nodes using in gallery */
function removeGalleryItems()
{
  var studio_bgr = $('#studio_bgr');
  studio_bgr.remove();
  var studio_panel = $('#studio_panel');
  studio_panel.remove();  
}


/** downloading gallery thumbs via AJAX */
function downloadGallery(id, i, sParams)
{
  // POKAZAC LOADING
  $('#studio_panel').fadeTo(50,0.6);
  var conteiner = $('#extra_content');
  conteiner.append('<div id="studio_loading"><img id="studio_spinner" src="src/img/loading-main.gif" /><br />' +
                    ' Trwa ładowanie strony, proszę czekać..</div>');
                    
  var margin = window.getScrollY()+ (myClientHeight() / 2)-20;
  $('#studio_loading').css('top',margin+'px');                    
  //return;
  
	var params = 
	{
		id: id,
		part: i
	};
	
	$.ajax(
	{
		data: params,
		type: 'post',
		url: 'ajax.php?function=getGallery',
		success: function (response) {
			onSuccess(response, id, sParams);
		},
		error: onNotSuccess
	});  
}


/** changing current gallery to next */
function nextGallery(id)
{
  //alert('tu bedzie sie ladowac next gallery '+id);

  var studio_panel = $('#studio_panel');

	var params = 
	{
		showPrev: true,
		printThumbList: true,
		createBgr: true
	};

  studio_panel.fadeOut(500, function()
            {
                $('#studio_panel').remove();
                downloadGallery(id,1,params);
            }  );
  return false;
}

function onSuccess(response, id, params)
{
  var resp = eval('(' + response +')');
  //ustawienie tla
 
  if (params['createBgr'] == true)
  {  
    var conteiner = $('#extra_content');
    conteiner.append('<div id="studio_panel">' +
                       '<div id="studio_box">' + 
                        '<div id="studio_preview">' +
                          '<div id="prev_loading"><img src="img/loadingBar.gif" /></div>' +
                        '</div>' +
                        '<div id="studio_thumbs">' +
                          '<span id="studio_title">' + resp['title'] + '</span>' +
                          '<ul id="studio_thumbs_list"></ul>' +
                        '</div>' +
                       '</div>' +
                     '</div>');
        $('#studio_preview').append('')
    var studio_panel = $('#studio_panel');
    var margin = window.getScrollY()+ (myClientHeight() / 2) - 315;
    var bgr_color = resp['color'];
    studio_panel.css('height','630px');  
    studio_panel.css('background',bgr_color);
    studio_panel.css('top',margin+'px');
    studio_panel.fadeIn(800);
    
    var studio_thumbs = $('#studio_thumbs');
    var studio_box = $('#studio_box');
    studio_box.append('<div id="gallery_close"></div>' +
                         '<a id="gallery_close_a" href="" onclick="closeGallery(); return false;">zamknij galerię</a>');
    studio_box.append('<div id="gallery_next"></div>' + 
                         '<a id="gallery_next_a" href="" '+'onclick="nextGallery('+resp['nextGallId']+'); return false;">'+
                                                      'następna galeria</a>');
     var studio_preview = $('#studio_preview');                                                      
     studio_preview.append('<img class="studio_img" src="" />');
  }
  
  var studio_preview = $('#studio_preview');
  var studio_thumbs = $('#studio_thumbs');
  var studio_thumbs_list = $('#studio_thumbs_list');
  var items = resp['items'];
  
  if (items.length == 0)
  {
    alert('Ta strona jest jeszcze w trakcie budowy, zapraszamy wkrótce.');
    closeGallery();
    return;
  }
  
  if (params['printThumbList'])
  {
    //drukowanie listy miniaturek - TU przygotowac juz w php ikony dalej - wstecz - razem z onClick itd, tu tylko wydrukowac
    
    //kasowanie ewentualnych elementow listy
    var listNodes = $(".thumb_list_li");
    listNodes.each( function()
              {
                $(this).remove();
              }
              );
    
    var part = parseInt(resp['part']);
    if (resp['prev'])
    {
      studio_thumbs_list.append('<li class="thumb_list_li"><div id="gallery_list_prev"></div>' +
                                '<a id="gallery_list_prev_a" href="" onclick="changeThumbList('+id+','+(part-1)+'); return false;">'+
                                '</a></li>');
    }      
  
    for (var i=0; i<items.length; i++)
    {
      studio_thumbs_list.append('<li class="thumb_list_li"><a href="" onclick="'+items[i]['onClick']+'">' +
                                        '<img src="' + items[i]['thumb_src'] + '" />' +
                                '</a></li>');
    }
  
    if (resp['next'])
    {
      studio_thumbs_list.append('<li class="thumb_list_li"><div id="gallery_list_next"></div>' +
                              '<a id="gallery_list_next_a" href="" onclick="changeThumbList('+id+','+(part+1)+'); return false;">' +
                              '</a></li>');
    }      
  }    
    
  if (params['showPrev'] == true)
    showFoto(items[0]['id']);
    //studio_preview.append('<img id="studio_img" src="' + items[0]['prev_src'] + '" />');
  
  $('#studio_loading').remove();
  $('#studio_panel').fadeTo(50,1);
}

function x(timeoutId)
{
  clearTimeout(timeoutId);
  $('#prev_loading').fadeOut();
  
  if ($('#to_be_delete').attr('id') != 'to_be_delete')
    return;  
    
  $('#to_be_delete').fadeOut().remove();
  $('#new_img').fadeIn(500);
}

/** shows preview of foto with given id */
function showFoto(id)
{
  var div = $('#studio_preview');
  var img = $('.studio_img');
  //alert(img.css('display'));
  
  //pierwszy obraz po otwarciu galerii bedzie sie ładował bedac display none, kolejne juz beda mialy inline
  if (img.css('display') == 'inline')
    img.fadeTo(500, 0.3);
                                               // img/loading-bar.gif         js/jquery_lightbox/images/loading.gif
  var timeoutId = setTimeout(function(){$('#prev_loading').fadeIn(); } , 800);

  img.attr('id','to_be_delete');
    
  img = $('#to_be_delete');
  
                                                                               //type=p            //onload for IE
  div.append('<img id="new_img" class="studio_img"  onload="x('+timeoutId+');" />');
  $('#new_img').attr('src','cmd.php?cmd=img&class=pageimage&type=p&id='+id);

  var new_img = $('#new_img');

  // on load event for FF  
  new_img.load(function()  { x(timeoutId);});  
}



/** changing thumb list */
function changeThumbList (id, part)
{
  //alert('changing thumb list to part: '+part);
	
  var params = 
	{
		showPrev: false,
		printThumbList: true,
		createBgr: false
	};

  downloadGallery(id, part, params);

  return false;
}

function onNotSuccess ()
{
}


/** function that brings up an overlay and opens swf pano */
function showPano()
{
  var conteiner = $('#extra_content');
  var clientHeight = myClientHeight();
  var clientWidth = myClientWidth();
  
  conteiner.css('height',$('body').height()+'px');
  conteiner.css('width','100%');
  conteiner.css('display','block');
  
  conteiner.append('<div id="pano_bgr"></div>');
  
  var studio_bgr = $('#pano_bgr');
  studio_bgr.css('opacity',0);

  studio_bgr.fadeTo(500,0.9);
  conteiner.append('<div id="pano_panel">' +
                     '<div id="pano_box">' + 
                     '</div>' +
                   '</div>');

  var pano_panel = $('#pano_panel');
  var margin = window.getScrollY()+ (myClientHeight() / 2) - 315;

  pano_panel.css('height','630px');  
  pano_panel.css('background','#000');
  pano_panel.css('top',margin+'px');
  pano_panel.fadeIn(800);
  
  var pano_box = $('#pano_box');
  pano_box.append('<div id="pano_close"></div>' +
                       '<a id="pano_close_a" href="" onclick="closePano(); return false;">zamknij</a>');

  pano_box.append('<object id="pano_swf" data="media/laboratorium/3maja_out.swf" type="application/x-shockwave-flash">' +
                     '<param name="src" value="media/laboratorium/3maja_out.swf" />' +
                  '</object>');
  
  return false;
}

/** fadeing out pano panel */
function closePano()
{
  var conteiner = $('#extra_content');
  conteiner.fadeOut(500, removePano);
}


/** removing dynamicly loaded DOM nodes using in pano */
function removePano()
{
  $('#pano_bgr').remove();
  $('#pano_panel').remove();  
}


/** return width of browser client area */
function myClientWidth() 
{
  var myWidth = 0;
  if( typeof( window.innerWidth ) == 'number' ) 
  {
    //Non-IE
    myWidth = window.innerWidth;
  } 
  else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) 
  {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
  } 
  else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) 
  {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
  }
  return myWidth;
}

/** return height of browser client area */
function myClientHeight() 
{
  var myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) 
  {
    //Non-IE
    myHeight = window.innerHeight;
  } 
  else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) 
  {
    //IE 6+ in 'standards compliant mode'
    myHeight = document.documentElement.clientHeight;
  } 
  else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) 
  {
    //IE 4 compatible
    myHeight = document.body.clientHeight;
  }
  return myHeight;
}


/* -- END STUDIO -- */


