//fleXenv.initByClass("flexcroll");


$(document).ready(function() {
	// do stuff when DOM is ready
  
  
  $("body").css({
    "overflow":"hidden"
  }); 
  
  fleXenv.initByClass("flexcroll");
  
  
  

	var blocks = new Array();
	$('.portfolio-info').each(function () {
		blocks.push(
			{cssid: $(this).attr('id'), 
			portfolioid: $(this).find('.portfolioid').html(),
      photo:$(this).find('.photo-url').html()}
		);
    //alert($(this).attr('id'));
	});
  
	// https://github.com/allmarkedup/jQuery-URL-Parser
	var portfolioid = jQuery.url.attr('anchor');
	// now load the correct picture
	var index = loadRoom(portfolioid);
		
	for (var i =0 ;i<blocks.length; i++) {
		//alert(index);alert(i);
		if (i == index) {
			$("#"+blocks[i].cssid).show();
      $("body").ezBgResize({
        img		: "uploads/showrooms/alb_"+blocks[i].portfolioid+"/"+blocks[i].photo, // Relative path example.  You could also use an absolute url (http://...).
        opacity : 1, // Opacity. 1 = 100%.  This is optional.
        center  : false // Boolean (true or false). This is optional. Default is true.
      });	
		}
		else {
			$("#"+blocks[i].cssid).hide();
		}
	}
	
  $('#pagerinfo').html((index+1)+"/"+blocks.length);
	parent.location.hash = blocks[index].portfolioid;
  
  
  
  
  
  function loadRoom(portfolioid) {
		// first look at the order of the divs
		var l = blocks.length;
		var index = 0;
		for (var i = 0; i<l; i++) {
			if (blocks[i].portfolioid == portfolioid) {
			index = i;
				break;
			}
		}
		return index;
	}
  
  function loadBlockIdByPortfolioId(portfolioid) {
		var index = loadRoom(portfolioid);
		return '#'+blocks[index].cssid;
	}
	
	function loadBlockIdByIndex(index) {
		return '#'+blocks[index].cssid;
	}
  
  if (index == 0) $("#linkgoleft").hide();
	if (index == blocks.length-1) $("#linkgoright").hide();
	if (index > 0) $('#linkgoleft').attr('href','#'+blocks[index-1].portfolioid);
	if (index < blocks.length-1) { $('#linkgoright').attr('href','#'+blocks[index+1].portfolioid); }
  
  
  
  $('#linkgoright').click(function() {
		if (index < blocks.length-1) {
			$('#linkgoleft').show();
			
			index++;
			if (index == blocks.length-1) {
				$(this).hide();
			} else {
				$('#linkgoright').attr('href','#'+blocks[index+1].portfolioid);
			}
			$('#linkgoleft').attr('href','#'+blocks[index-1].portfolioid);
		
			// prepare next block
			var cb = loadBlockIdByIndex(index-1);//'#block'+(num-1);
			var nb = loadBlockIdByIndex(index);//'#block'+num;

			$(nb).css('left', '2000px');
      
      //$("#jq_ez_bg").fadeOut('slow');
      
      
      setTimeout(function() { 
      
        $("#jq_ez_bg").fadeOut(400, function() {
          $("body").ezBgResize({
            img		: "uploads/showrooms/alb_"+blocks[index].portfolioid+"/"+blocks[index].photo, // Relative path example.  You could also use an absolute url (http://...).
            opacity : 1, // Opacity. 1 = 100%.  This is optional.
            center  : false // Boolean (true or false). This is optional. Default is true.
          });	
        });
      
      }, 600);
      
      
      
			$(cb).animate({left:'-2000px'}, {queue: false, duration: 600}, function() {
			});
			//$(cb).fadeOut(600);
			
			$(nb).animate({left:'0px'}, {queue: false, duration: 600}, function() {
			});
			//$(nb).fadeIn(600);
			$(nb).show();
      
      $('#pagerinfo').html((index+1)+"/"+blocks.length);
			fleXenv.initByClass("flexcroll");
		}
		
		parent.location.hash = blocks[index].portfolioid;
		return false;
	});
	

  $('#linkgoleft').click(function() {
		if (index > 0) {
			$('#linkgoright').show();
			index--;
			if (index == 0) {
				$(this).hide();
			} else {
				$('#linkgoleft').attr('href','#'+blocks[index-1].portfolioid);
			}
			$('#linkgoright').attr('href','#'+blocks[index+1].portfolioid);
		
			// prepare next block
			var cb = loadBlockIdByIndex(index+1);//'#block'+(num+1);
			var nb = loadBlockIdByIndex(index);//'#block'+num;

			$(nb).css('left', '-2000px');
			
      
      setTimeout(function() { 
      
        $("#jq_ez_bg").fadeOut(400, function() {
          $("body").ezBgResize({
            img		: "uploads/showrooms/alb_"+blocks[index].portfolioid+"/"+blocks[index].photo, // Relative path example.  You could also use an absolute url (http://...).
            opacity : 1, // Opacity. 1 = 100%.  This is optional.
            center  : false // Boolean (true or false). This is optional. Default is true.
          });	
        });
      
      }, 600);

      
			
			$(cb).animate({left:'2000px'}, {queue: false, duration: 600}, function() {
			});
			//$(cb).fadeOut(600);
			
			$(nb).animate({left:'0px'}, {queue: false, duration: 600}, function() {
			});
			//$(nb).fadeIn(600);
      $(nb).show();
			
      //alert(blocks[index].photo);
      $('#pagerinfo').html((index+1)+"/"+blocks.length);
      
      
			fleXenv.initByClass("flexcroll");
		}
		
		parent.location.hash = blocks[index].portfolioid;
		return false;
	});
  
  
  
  
  
  
  /*
	fleXenv.initByClass("flexcroll");

	var blocks = new Array();
	$('.infoBlock').each(function () {
		blocks.push(
			{cssid: $(this).attr('id'), 
			roomid: $(this).find('.roomid').html()}
		);
	});
	// https://github.com/allmarkedup/jQuery-URL-Parser
	var roomid = jQuery.url.attr('anchor');
	
	// now load the correct picture
		
	var index = loadRoom(roomid);
		
	for (var i =0 ;i<blocks.length; i++) {
		//alert(index);alert(i);
		if (i == index) {
			$("#"+blocks[i].cssid+" .rightBlock").show();
			$("#"+blocks[i].cssid+" .leftBlock").show();
		}
		else {
			$("#"+blocks[i].cssid+" .rightBlock").hide();
			$("#"+blocks[i].cssid+" .leftBlock").hide();
		}
	}
	
	parent.location.hash = blocks[index].roomid;
	
	function loadRoom(roomid) {
		// first look at the order of the divs
		var l = blocks.length;
		var index = 0;
		for (var i = 0; i<l; i++) {
			if (blocks[i].roomid == roomid) {
			index = i;
				break;
			}
		}
		return index;
	}
	
	if (index == 0) $("#linkgoleft").hide();
	if (index == blocks.length-1) $("#linkgoright").hide();
	if (index > 0) $('#linkgoleft').attr('href','#'+blocks[index-1].roomid);
	if (index < blocks.length-1) { $('#linkgoright').attr('href','#'+blocks[index+1].roomid); }
	
	
	
	
	function loadBlockIdByRoomId(roomid) {
		var index = loadRoom(roomid);
		return '#'+blocks[index].cssid;
	}
	
	function loadBlockIdByIndex(index) {
		return '#'+blocks[index].cssid;
	}
	
	
	$('#linkgoright').click(function() {
		if (index < blocks.length-1) {
			$('#linkgoleft').show();
			
			index++;
			if (index == blocks.length-1) {
				$(this).hide();
			} else {
				$('#linkgoright').attr('href','#'+blocks[index+1].roomid);
			}
			$('#linkgoleft').attr('href','#'+blocks[index-1].roomid);
		
			// prepare next block
			var cb = loadBlockIdByIndex(index-1);//'#block'+(num-1);
			var nb = loadBlockIdByIndex(index);//'#block'+num;

			$(nb).css('left', '860px');
			
			$(cb).animate({left:'-860px'}, {queue: false, duration: 600}, function() {
			});
			$(cb+" .leftBlock").fadeOut(600);
			$(cb+" .rightBlock").fadeOut(600);
			$(cb+" .rightBlock .vergrootglas").css('display','none');
			
			$(nb).animate({left:'0px'}, {queue: false, duration: 600}, function() {
			});
			$(nb+" .leftBlock").fadeIn(600);
			$(nb+" .rightBlock").fadeIn(600);
			$(nb+" .rightBlock .vergrootglas").css('display','block');
			
			fleXenv.initByClass("flexcroll");
			//setTimeout('window.location.reload()', 600);
		}
		
		parent.location.hash = blocks[index].roomid;
		return false;
	});
	

  $('#linkgoleft').click(function() {
		if (index > 0) {
			$('#linkgoright').show();
			index--;
			if (index == 0) {
				$(this).hide();
			} else {
				$('#linkgoleft').attr('href','#'+blocks[index-1].roomid);
			}
			$('#linkgoright').attr('href','#'+blocks[index+1].roomid);
		
			// prepare next block
			var cb = loadBlockIdByIndex(index+1);//'#block'+(num+1);
			var nb = loadBlockIdByIndex(index);//'#block'+num;

			$(nb).css('left', '-860px');
			
			
			$(cb).animate({left:'860px'}, {queue: false, duration: 600}, function() {
			});
			$(cb+" .leftBlock").fadeOut(600);
			$(cb+" .rightBlock").fadeOut(600);
			$(cb+" .rightBlock .vergrootglas").css('display','none');
			
			$(nb).animate({left:'0px'}, {queue: false, duration: 600}, function() {
			});
			$(nb+" .leftBlock").fadeIn(600);
			$(nb+" .rightBlock").fadeIn(600);
			$(nb+" .rightBlock .vergrootglas").css('display','block');

			
			fleXenv.initByClass("flexcroll");
			
			//setTimeout('window.location.reload()', 600);
		}
		
		parent.location.hash = blocks[index].roomid;
		return false;
	});

	*/
	
	
	
	
   
});

