function addLoadEvent(func) {
    var oldonload = window.onload;
    if (typeof window.onload != 'function') {
        window.onload = func;
    } else {
        window.onload = function() {
            oldonload();
            func();
        }
    }
}

function moveSlideshow(elementID,final_x,final_y,interval) {
    if (!document.getElementById) return false;
    
    // if the element does not exist we have nothing to do
    if (!document.getElementById(elementID)) return false;
    var elem = document.getElementById(elementID);
    
    // the slideshow events stack up and the animation is not smooth anymore
    if (elem.movement) {
        clearTimeout(elem.movement);
    }
    
    // current slideshow position
    var xpos = parseInt(elem.style.left);
    var ypos = parseInt(elem.style.top);
    if (xpos == final_x && ypos == final_y) {
        return true;
    }
    
    // restrict moving to white area
    if (final_x <= -elem.max_x) {
        final_x = -elem.max_x;
    }
    if (final_x > 0) {
	    final_x = 0;
    }
    
    // animation bit (taken from the book DOM Scripting by Jeremy Keith)
    if (xpos < final_x) {
        var dist = Math.ceil((final_x - xpos)/10);
        xpos = xpos + dist;
    }
    if (xpos > final_x) {
        var dist = Math.ceil((xpos - final_x)/10);
        xpos = xpos - dist;
    }
  
    // again, restrict showing white area
    if (xpos <= -elem.max_x) {
	    xpos = -elem.max_x;
    }
    if (xpos > 0) {
	    xpos = 0;
    }
    
    // fix the elements position
    elem.style.left = xpos + "px";
    elem.style.top = ypos + "px";
    
    // and set up the event again after an interval
    var repeat = "moveSlideshow('"+elementID+"',"+final_x+","+final_y+","+interval+")";
    elem.movement = setTimeout(repeat,interval);
}

function prepareSlideshow(id) {
	// first lets make sure the browser understands the DOM methods we will be using
  	if (!document.getElementsByTagName) return false;
  	if (!document.getElementById) return false;
  	
	// Make sure the elements exist
  	if (!document.getElementById("slideshow"+id)) return false;
  	var slideshow = document.getElementById("slideshow"+id);
  	var wrapper = document.getElementById("slideshow_wrapper"+id);
  	wrapper.style.overflow = "hidden";
  	
  	// prepare the navigation bit we will be using
  	// left
  	var navigation = document.createElement("ul");
  	navigation.setAttribute("id", "navigation");
  	var li = document.createElement("li");
  	var scroll_left = document.createElement("a");
  	scroll_left.setAttribute("id", "scroll_left");
  	scroll_left.href ="#";
  	var text = document.createTextNode("Left");
  	scroll_left.appendChild(text);
  	li.appendChild(scroll_left);
  	navigation.appendChild(li);
  	slideshow.insertBefore(navigation, wrapper);
  	
  	//right
  	var li = document.createElement("li");
  	var scroll_right = document.createElement("a");
  	scroll_right.setAttribute("id", "scroll_right"+id);
  	scroll_right.setAttribute("class", "scroll_right");
  	scroll_right.href ="#";
  	var text = document.createTextNode("");
  	scroll_right.appendChild(text);
  	li.appendChild(scroll_right);
  	navigation.appendChild(li);
  	slideshow.insertBefore(navigation, wrapper);
	
	var slideshow_set = document.getElementById("slideshow_set"+id);
	slideshow_set.style.top = 0+"px";
	slideshow_set.style.left = 0+"px";
	
	// to get the max y position of the gallery image track we need to count all
	// the li items and multiply that number with 130
	var li = slideshow_set.getElementsByTagName("li");
	slideshow_set.max_x = (li.length-1) * 330;
	slideshow_set.max_y = li.length * 130;
	
	// need the width of the gallery so that they do not scroll vertical
	var width = li.length * 330;
	slideshow_set.style.width = width + "px";
	//CONFIG DEFAULT PAGE
	var position = 0;
	var defaul_item = 4;
	
	// Attach onmouseover event for left
  	scroll_left.onclick = function() {
		// get the current position of the gallery element
		var four_image_width = 330;
		var slideshow_set = document.getElementById("slideshow_set"+id);
		var x = parseInt(slideshow_set.style.left);
		
		var page_object = document.getElementById("slideshow_wrapper_page"+id);
		var current_page = parseInt(page_object.value);
		if(x != 0){
			
			if (x % four_image_width == 0) {
	    		moveSlideshow("slideshow_set"+id,x+four_image_width,0,10);
	    		page_object.value = parseInt(page_object.value) - 1 ;
			}
			
			if(x != four_image_width){
				four_image_width = -(current_page-2)*four_image_width;
				moveSlideshow("slideshow_set"+id,four_image_width,0,10);
	    		page_object.value = current_page - 1 ;
			}
		}
		/*
		backup
		if (x % 330 == 0) {
    		moveSlideshow("slideshow_set"+id,x+four_image_width,0,10);
		}*/
		return false;
	}
	
	// Attach onmouseover event for right
  	scroll_right.onclick = function() {
		// get the current position of the gallery element
		var four_image_width = 330;
		var slideshow_set = document.getElementById("slideshow_set"+id);
		var page_object = document.getElementById("slideshow_wrapper_page"+id);
		var current_page = parseInt(page_object.value);
		
		var len_img = li.length;//alert((leng_img/2)*80);
		var total_page = Math.ceil(len_img/defaul_item);
		var x = parseInt(slideshow_set.style.left);
		
		total_page = parseInt(total_page);
		len_img = parseInt(len_img);
		
		var temp= total_page - current_page;
		if(temp > 0){
			var miss_item = len_img - (current_page*defaul_item);
			var added_item = defaul_item - parseInt(miss_item); 
			
			if(added_item > 0){
				var width_one_item = parseInt(Math.ceil(four_image_width/defaul_item));
				four_image_width =  parseInt(width_one_item*miss_item);
				
			}
			//four_image_width = 330;
			moveSlideshow("slideshow_set"+id,x-four_image_width,0,10);
	    	$('#scroll_right'+id).css('display','none');
	    	setTimeout("enable_right_button("+id+")",500);
	    	page_object.value = current_page +1 ;
		}
		
		/*if (x > -((total_page-1)*four_image_width) ) {
	    		moveSlideshow("slideshow_set"+id,x-four_image_width,0,10);
	    		$('#scroll_right'+id).css('display','none');
	    		setTimeout("enable_right_button("+id+")",500);
	    		page_object.value = parseInt(page_object.value) +1 ;
	
		}*/
		return false;
	}
}

function enable_right_button(id){
	$('#scroll_right'+id).css('display','block');
}

function msieversion()
   {
      var ua = window.navigator.userAgent
      var msie = ua.indexOf ( "MSIE " )

      if ( msie > 0 )      // If Internet Explorer, return version number
         return parseInt (ua.substring (msie+5, ua.indexOf (".", msie )))
      else                 // If another browser, return 0
         return 0

   }

function displayLarge(id_list_img,id_main_img){
	$('#main_img_accommodation'+id_main_img).attr('src', $('#img_list_'+id_list_img).attr('name'));	
	document.getElementById('acc_image_id'+id_main_img).value = id_list_img;
	document.getElementById('viewlarger_mainimgage'+id_main_img).value= id_main_img +''+ id_list_img;
	var count_img = $('#list_img_by_acc'+id_main_img).val();
	var arr = count_img.split(',');
	for(var i =0;i<arr.length;i++)
	{
		if(arr[i] == id_list_img){
			document.getElementById('thumnail_'+arr[i]).style.display = 'block';
			document.getElementById('main_thumnail_'+arr[i]).style.display = 'block';
			$('#main_img_accommodation'+arr[i]).attr('src', $('#img_list_'+id_list_img).attr('name'));	
			$('#main_image'+arr[i]).attr('href', $('#img_list_'+id_list_img).attr('name').replace('original/',''));
		}
		else{
			document.getElementById('thumnail_'+arr[i]).style.display = 'none';
			document.getElementById('main_thumnail_'+arr[i]).style.display = 'none';
		}
	}
}
function showslide(id)
{
		showslideimage(id);
		$('#floor_plan_image'+id).click();
}
function view_larger_main_image(main_id)
{
	var id = document.getElementById('viewlarger_mainimgage'+main_id).value;
	showslideimage(id);
	$('#floor_plan_image'+id).click();
}
function open_formular_email(title,acc_id) {
	var l = Math.round(($(document).width() - 710)/2); 
	document.getElementById('title_des').innerHTML = 'Thank you for recommending the following accommodation unit to a friend: <strong>'+title+'</strong>';
	document.getElementById('m_acc_id').value = acc_id;
	document.getElementById('m_acc_image_id').value = $('#acc_image_id'+acc_id).val();
	$('#your_name').val("") ;
	$('#your_email').val("");
	$('#friend_name').val("");
	$('#friend_email').val("");
	$('#message').val("");
	$('#captchaword').val("");
	$.blockUI({ message: $('div#formular_email'),css:{width:'45%', top:'10%', left:l+'px'} });	
}
function print_accommodation_type(acc_id)
{
	var acc_img_id = $('#acc_image_id'+acc_id).val();
	newwindow = window.open(index()+'modules.accommodation_unit.print_accommodation_type/'+acc_id+'/'+acc_img_id,'test','width=890,address=0,height=550,toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,copyhistory=0,resizable=1,top=80,left=200');


}

function close_block()
{
	$.unblockUI();   
}
function open_latest_special() {
	var l = Math.round(($(document).width() - 710)/2);
	$.blockUI({ message: $('div#latest_special'),css:{width:'45%', top:'10%', left:l+'px'} });	
}
function showslideimage(id)
{
	$(document).ready(function() {
		//show all image except image floor plan
		$('a.lightbox_image'+id).lightBox({
			'fixedNavigation' : 'true', //set button next and previous always on
			'imageLoading':			lightbox_image_path + 'lightbox-ico-loading.gif',		// (string) Path and the name of the loading icon
			'imageBtnPrev':			lightbox_image_path + 'lightbox-btn-prev.gif',			// (string) Path and the name of the prev button image
			'imageBtnNext':			lightbox_image_path + 'lightbox-btn-next.gif',			// (string) Path and the name of the next button image
			'imageBtnClose':		lightbox_image_path + 'lightbox-btn-close.gif',		// (string) Path and the name of the close btn
			'imageBlank':			lightbox_image_path + 'lightbox-blank.gif'
		});
	});
} 
