/* DEFAULT PARAMETERS */
var txt_div= 'txt';
var detail_div= 'detail';
var detail_div_class = 'caraousel_detail_enlarge_v';
var carouse_style='vertical_carousel';
var txt_class = 'ousel_detail_text_v';
var carousel_class = 'caraousel_v';
var numbers_div = 'itemnumber';
var numbers_div_class = 'itemnumber_v';
var get_image_src='';
var detail_height = 195;
var detail_width = 400;
var current_dir = 'interface';
/*			FINE 			 */

var current = '';
var counter_nw=1;
var current_zoom = 1;
var current_page = "";
var max_x;
var max_y;
var min_x; 
var min_y; 
var inc_x; 
var inc_y; 
var current_1x = "";
var current_2x = "";
var current_3x = "";
var menu_array; // = array();


function ik_vertical_carousel(main_dir,img_subdir, carousel_div, text_div, img_detail_div, img_detail_div_class, img_detail_height, img_detail_width,text_class,car_class,car_style,num_div,num_class){
	txt_div=text_div;
	txt_class=text_class;
	carousel_class=car_class;
	detail_div=img_detail_div;
	detail_div_class=img_detail_div_class;
	detail_height=img_detail_height;
	detail_width=img_detail_width; 
	carouse_style=car_style;
	numbers_div = num_div;
	numbers_div_class=num_class;
	current_dir = main_dir;
	create_carousel(img_subdir,carousel_div,true,text_class,car_class);
}

function ik_horizontal_carousel(main_dir, img_subdir, carousel_div, text_div, img_detail_div, img_detail_div_class, img_detail_height, img_detail_width,text_class,car_class,car_style,num_div,num_class){
   txt_div=text_div;
	txt_class=text_class;
   carousel_class=car_class;
   detail_div=img_detail_div;
   detail_div_class=img_detail_div_class;
   detail_height=img_detail_height;
   detail_width=img_detail_width;
	carouse_style=car_style;
	numbers_div = num_div;
	numbers_div_class=num_class;
	current_dir = main_dir;
   create_carousel(img_subdir,carousel_div,false);
}

function ik_generic_carousel(main_dir, img_subdir, carousel_div, img_detail_div, img_detail_div_class, img_detail_height, img_detail_width, car_class, car_style, num_div, num_class){
   carousel_class=car_class;
   detail_div=img_detail_div;
   detail_div_class=img_detail_div_class;
   detail_height=img_detail_height;
   detail_width=img_detail_width;
	carouse_style=car_style;
	numbers_div = num_div;
	numbers_div_class=num_class;
	current_dir = main_dir;
   create_carousel_generic(img_subdir,carousel_div,false);
}

function create_carousel_generic(pag,div,verticalf){
   $(div).innerHTML="";
   $(detail_div).innerHTML="";
//   $(txt_div).className = txt_class;
	
   $(div).className = carousel_class;
   $(detail_div).className = detail_div_class;

   new Ajax.Request('../switch_server.php?page=' + pag +'&path='+current_dir, {
      method:'get',
      onSuccess: function(transport){
         current_zoom = 1;
         current_page = pag;
         current_1x = "";
         current_2x = "";
         current_3x = "";
         var list = transport.responseText.evalJSON();
         render_carousel_generic(list,pag,div,verticalf);
         }
   });
}

function render_carousel_generic(list,pag,div,verticalf){
   str = '\n\n<div id="'+carouse_style+'">\n\t<div class="previous_button"></div>\n\t<div class="container">\n\t\t<ul>';
   max = list['num'];
   for(i = 0; i < list['num']; i++){
      numj=i+1;
      pos = numj+'/'+max;
      on_click = get_onclick_actions_generic(pag, list[i]['txt'], list[i]['small'], list[i]['big_x1'], list[i]['big_x2'], list[i]['big_x3'],list[i]['name'],pos);
      str +=   '\n\t\t\t<li>' +
               '\n\t\t\t\t<img alt="Image '+numj+'/'+max+'" src="'+list[i]['small']+'" '+ on_click +' >'+
               '<br/>'+numj+'/'+max+'\n\t\t\t</li>';
   }
   str += '\n\t\t</ul>\n\t</div>\n\t<div class="next_button"></div>\n</div>\n\n';
   $(div).innerHTML = str;
   if(verticalf){
      new UI.Carousel(carouse_style, {direction: "vertical", nbVisible: 4.0});
      show_txt(list[0]['txt'],txt_div);
      show_img(list[0]['big_x1'],detail_div,list[0]['big'],list[i]['big_x3']);
      $(numbers_div).className=numbers_div_class; $(numbers_div).innerHTML='1/'+max;
      set_defaults(pag,list[0]['name'],list[0]['big_x1'],list[0]['big_x2'],list[0]['big_x3'],detail_height,detail_width);
   }else{
      new UI.Carousel(carouse_style);
      show_txt(list[0]['txt'],txt_div);
      show_img(list[0]['big_x1'],detail_div,list[0]['big'],list[i]['big_x3']);
      $(numbers_div).className=numbers_div_class; $(numbers_div).innerHTML='1/'+max;
      set_defaults(pag,list[0]['name'],list[0]['big_x1'],list[0]['big_x2'],list[0]['big_x3'],detail_height,detail_width);
   }
}




function create_carousel(pag,div,verticalf){
	$(div).innerHTML="";
	$(detail_div).innerHTML="";	
	$(txt_div).className = txt_class;
	$(div).className = carousel_class;
	$(detail_div).className = detail_div_class;

   new Ajax.Request('../switch_server.php?page=' + pag +'&path='+current_dir, {
      method:'get',
      onSuccess: function(transport){
         current_zoom = 1;
         current_page = pag;
			current_1x = "";
			current_2x = "";
			current_3x = "";
			var list = transport.responseText.evalJSON();
			render_carousel(list,pag,div,verticalf);
         }
   });
}

function render_carousel(list,pag,div,verticalf){
	str = '\n\n<div id="'+carouse_style+'">\n\t<div class="previous_button"></div>\n\t<div class="container">\n\t\t<ul>';
	max = list['num'];
	for(i = 0; i < list['num']; i++){
		numj=i+1;
		pos = numj+'/'+max;
		on_click = get_onclick_actions(pag, list[i]['txt'], list[i]['small'], list[i]['big_x1'], list[i]['big_x2'], list[i]['big_x3'],list[i]['name'],pos,list[i]['big']);
		str +=   '\n\t\t\t<li>' +
					'\n\t\t\t\t<img alt="Image '+numj+'/'+max+'" src="'+list[i]['small']+'" '+ on_click +' >'+
					'<br/>'+numj+'/'+max+'\n\t\t\t</li>';
	}
	str += '\n\t\t</ul>\n\t</div>\n\t<div class="next_button"></div>\n</div>\n\n';
	$(div).innerHTML = str;
	if(verticalf){
		new UI.Carousel(carouse_style, {direction: "vertical", nbVisible: 4.0});
		show_txt(list[0]['txt'],txt_div);
		show_img(list[0]['big_x1'],detail_div,list[0]['big'],list[0]['big_x3']);
		$(numbers_div).className=numbers_div_class; $(numbers_div).innerHTML='1/'+max;
		set_defaults(pag,list[0]['name'],list[0]['big_x1'],list[0]['big_x2'],list[0]['big_x3'],detail_height,detail_width);
	}else{
		new UI.Carousel(carouse_style);
		show_txt(list[0]['txt'],txt_div);
      show_img(list[0]['big_x1'],detail_div,list[0]['big'],list[0]['big_x3']);
		$(numbers_div).className=numbers_div_class; $(numbers_div).innerHTML='1/'+max;
      set_defaults(pag,list[0]['name'],list[0]['big_x1'],list[0]['big_x2'],list[0]['big_x3'],detail_height,detail_width);
	}
}

function get_onclick_actions_generic(pag, txt, small, big1x, big2x, big3x,name,pos,bigimg){
	out =  ' style="cursor:pointer;" ';
	out += ' onClick=" show_img(\''+big1x+'\',\''+detail_div+'\',\''+bigimg+'\',\''+big3x+'\'); ';
	out += ' $(\''+numbers_div+'\').className=\''+numbers_div_class+'\'; $(\''+numbers_div+'\').innerHTML=\''+pos+'\';';
	out += ' set_defaults(\''+pag+'\', \''+name+'\', \'' +big1x +'\', \'' +big2x +'\', \'' +big3x+'\',\''+detail_height+'\',\''+ detail_width+'\'); "';
	return out;
}

function get_onclick_actions(pag, txt, small, big1x, big2x, big3x,name,pos,bigimg){
   out =  ' style="cursor:pointer;" ';
   out += ' onClick="show_txt(\''+txt +'\',\''+txt_div+'\',\''+ pag +'\'); ';
   out += ' show_img(\''+big1x+'\',\''+detail_div+'\',\''+bigimg+'\',\''+big3x+'\'); ';
   out += ' $(\''+numbers_div+'\').className=\''+numbers_div_class+'\'; $(\''+numbers_div+'\').innerHTML=\''+pos+'\';';
   out += ' set_defaults(\''+pag+'\', \''+name+'\', \'' +big1x +'\', \'' +big2x +'\', \'' +big3x+'\',\''+detail_height+'\',\''+ detail_width+'\'); "';
   return out;
}


function show_txt(name,div){
   load('../interface_server.php', 'display=' + name +'&path='+current_dir, div);
}

function process(res){
      $(targhet_div).innerHTML=res.responseText;
};

function load(server, pars, targhet_div){
	new Ajax.Updater(
   	targhet_div, server + "?" + pars,{
            method: "GET", 
            onComplete: process
      	}
      )
};

function show_img(trg,div,big,img3x){
   $(div).innerHTML = "<a href='"+ img3x +"' rel='lightbox' title=''><img id='img1' src='" + trg + "'></a>";
   imgtd = $('img1');
   width_limit = imgtd.width;
   height_limit = imgtd.height;
   set_max(imgtd.width,imgtd.height,detail_width);
   imgtd.width = min_x;
	get_image_src=big;
}

function set_max(mxx,mxy,minx){
   max_x = mxx;
   max_y = mxy;
   min_x = minx;
   min_y = parseInt(min_x/max_x)*max_y;
   inc_x = 10;
   inc_y = parseInt(10/max_x) * max_y;
}

function set_defaults(i_page,i_name,i_x1,i_x2,i_x3,i_w,i_h){
   current_zoom = 1;
   current_1x = i_x1;
   current_2x = i_x2;
   current_3x = i_x3;
   current_page = i_page;
   current_name = i_name;
}

function get_download_link(){
   sr = get_image_src;
   window.open(sr,'newwin'+counter_nw);
   counter_nw++;
}

function zoom_in() {
	if(current_page != ""){
		if(current_zoom == 1){
			current_zoom = 2;
			$(detail_div).innerHTML = "<img id='img1' src='" + current_2x +"' style='cursor:move;'>";
			img_zoom2 = new Draggable('img1',{ ghosting: false, handle: detail_div, 
                     revert: function(){
                        x_p = 0;
                        y_p = 0;
                        if(   ($('img1').width + $('img1').offsetLeft) < detail_width  ){
                           x_p = detail_width - ($('img1').offsetLeft+$('img1').width);
                        }
                        if( ($('img1').width + $('img1').offsetLeft) > $('img1').width ){
                           x_p = $('img1').width - ($('img1').width + $('img1').offsetLeft);
                        }
                        if(   ($('img1').height + $('img1').offsetTop) >  $('img1').height   ){ 
                           y_p = $('img1').height - ($('img1').height + $('img1').offsetTop );
                        }
                        if( ($('img1').height + $('img1').offsetTop) < detail_height ){
                           y_p = detail_height - ($('img1').height + $('img1').offsetTop);
                        }
                        new Effect.Move('img1',{x: x_p, y: y_p, mode: 'relative', duration: 0.02 });
                     }
                  });
         Droppables.add(detail_div, { accept: detail_div_class, containment: detail_div, greedy: true});
		}else{
			if(current_zoom == 2){	
				current_zoom = 3;
				$(detail_div).innerHTML = "<img id='img1' src='" + current_3x +"' style='cursor:move;'>";
				img_zoom3 = new Draggable('img1',{ ghosting: false, handle: detail_div, 
                     revert: function(){
                        x_p = 0;
                        y_p = 0;
                        if(   ($('img1').width + $('img1').offsetLeft) < detail_width  ){
                           x_p = detail_width - ($('img1').offsetLeft+$('img1').width);
                        }
                        if( ($('img1').width + $('img1').offsetLeft) > $('img1').width ){
                           x_p = $('img1').width - ($('img1').width + $('img1').offsetLeft);
                        }
                        if(   ($('img1').height + $('img1').offsetTop) >  $('img1').height   ){ 
                           y_p = $('img1').height - ($('img1').height + $('img1').offsetTop );
                        }
                        if( ($('img1').height + $('img1').offsetTop) < detail_height ){
                           y_p = detail_height - ($('img1').height + $('img1').offsetTop);
                        }
                        new Effect.Move('img1',{x: x_p, y: y_p, mode: 'relative', duration: 0.12 });
                     }
                  });
         Droppables.add(detail_div, { accept: detail_div_class, containment: detail_div, greedy: true});
			}
		}
	}
}

function zoom_out() {
	if(current_page != ""){
		if(current_zoom == 3){
			current_zoom = 2;
			$(detail_div).innerHTML = "<img id='img1' src='" + current_2x +"'  style='cursor:move;'>";
			img_zoom2 = new Draggable('img1',{ ghosting: false, handle: detail_div,
                     revert: function(){
                        x_p = 0;
                        y_p = 0;
                        if(   ($('img1').width + $('img1').offsetLeft) < detail_width  ){
                           x_p = detail_width - ($('img1').offsetLeft+$('img1').width);
                        }
                        if( ($('img1').width + $('img1').offsetLeft) > $('img1').width ){
                           x_p = $('img1').width - ($('img1').width + $('img1').offsetLeft);
                        }
                        if(   ($('img1').height + $('img1').offsetTop) >  $('img1').height   ){
                           y_p = $('img1').height - ($('img1').height + $('img1').offsetTop );
                        }
                        if( ($('img1').height + $('img1').offsetTop) < detail_height ){
                           y_p = detail_height - ($('img1').height + $('img1').offsetTop);
                        }
                        new Effect.Move('img1',{x: x_p, y: y_p, mode: 'relative', duration: 0.02 });
                     }
                  });
         Droppables.add(detail_div, { accept: detail_div_class, containment: detail_div, greedy: true});
		}else{
			if(current_zoom == 2){	
				current_zoom = 1;
				$(detail_div).innerHTML = "<a href='"+ current_3x +"' rel='lightbox'><img id='img1' src='" + current_1x +"'></a>";
			}
		}
	}
}

function init_menu(m_array, menu_over_class, menu_out_class){
	over_class = menu_over_class;
   out_class = menu_out_class;
   menu_array = m_array;
}

function menu_click(dest_div){
	for (var i=0;i<menu_array.length;i++){
		if(menu_array[i] == dest_div){
			$(menu_array[i]).className = over_class;
			current = dest_div;
		}else{
			$(menu_array[i]).className = out_class;
		}
	}
}

function menu_over(dest_div){
	if(dest_div!=current){
   	$(dest_div).className=over_class;
   }
}

function menu_out(dest_div){
   if(dest_div!=current){
      $(dest_div).className=out_class;
   }
}



