// variables for popup windows
var profiles =
	{
		mapWindow:
		{
			height:550,width:750,toolbar:1,scrollbars:1,status:1,resizable:0,center:1
		},
		emailWindow:
		{
			height:380,width:430,toolbar:0,scrollbars:0,status:0,resizable:0,center:1
		},
		infoWindow:
		{
			height:380,width:750,toolbar:0,scrollbars:0,status:0,resizable:0,center:1
		}
	};
	
// get the id of this house
var homeID = $(document).getUrlParam('homeID');	
var dir = $('a#houseDir').attr('href');
var movie = $('#media-big').attr('href');

// document ready - let's roll!
$(document).ready(function() { 

$('#media-big').remove();

$('.popupwindow').popupwindow(profiles);

loadHome(homeID,dir);

// fire functions needing page load wait
buttonRoll();


// launch external links in new window
$('A[rel="external"]').click( function() {
        window.open( $(this).attr('href') );
        return false;
    });

}); 


// function to change hover states
function buttonRoll(){
 if ($('.btn') != null){
	$('.btn').hover(
      function () {
        $(this).css('background-position','left bottom');
      }, 
      function () {
        $(this).css('background-position','left top');
      }
    );
   }
}

// function for opening a modal box with video/audio
function loadHome(id,dir){
	if ( $('.movie').length ) {
		$('#gallery-image').html('<a id="media-big" href="'+movie+'" class="media-big"></a>');
		} else {
			$('#gallery-image').append('<div id="media-big" class="media-big" style="height:480px;"><div id="loader2" style="width:100px;height:100px; margin-left:auto; margin-right:auto; margin-top:145px;"><img src="2.0/_images/ajax-loader.gif" /></div></div>');
			$('#gallery-image').animate({'height': '480px'},500);
		$('#gallery-image').after('<ul id="gallery-menu"><li id="gm5" class="delay"><a class="btn" href="#" id="next">Next</a></li><li id="gm4" class="delay"><a class="btn" href="#" id="prev">Previous</a></li><li id="gm3" class="delay"><a class="btn" href="#" id="stop">Stop</a></li><li id="gm2" class="delay"><a class="btn" href="#" id="play">Play</a></li></ul>');
		 setTimeout(function(){getHousePics(dir,640,480,'#media-big');},2000);
			$('.delay').wait(6).then.slideDown(800);
			return;
		}
		$('.movie').html('<div id="loader2" style="width:100px;height:100px; margin-left:auto; margin-right:auto; margin-top:45px;"><img src="2.0/_images/ajax-loader.gif" /></div>');
		getHousePics(dir,310,233,'.movie'); 
		boxSwitch(movie,id,dir);
		$('.media-big').wait(3).then.media({width:640,height:545,bgColor:'#f6f6f6',autoplay:1});
}

function getHousePics(dir,w,h,div){
	$(div).wait(3).then.html('<img src="_sh_inc/image.php?src='+dir+'/image1.jpg&h='+h+'&w='+w+'&zc=1 width="'+w+'" height="'+h+'" /><img src="_sh_inc/image.php?src='+dir+'/image2.jpg&h='+h+'&w='+w+'&zc=1 width="'+w+'" height="'+h+'" />');
	$('#loader2').wait(2.6).then.fadeOut();
	$.post('_sh_inc/process.php', { location: dir, width:w, height:h, data_action: 'getHousePhotos' },
   		function(data){
   			$(div).wait(3.5).then.append(data);
   			setTimeout(function(){loadSlides(div)},3600);
   			});
}

function boxSwitch(movie,id,dir){
	$('#o-slider').append('<a href="#" id="slide-with" class="btn">Slider</a>').fadeIn();
	$('#slide-with').click(function(){
		$('#media-big embed,#media-big').animate({'width': '0px'},500).wait(1).then.css('height','0').empty();
		$('#slide-with').remove();
		$('.movie').cycle('stop').html('<div id="loader2" style="width:100px;height:100px; margin-left:auto; margin-right:auto; margin-top:45px;"><img src="2.0/_images/ajax-loader.gif" /></div>');
		$('#gallery-menu-left').empty();
		$('#gallery-image').animate({'height': '480px'},500);
		$('#gallery-image').after('<ul id="gallery-menu"><li id="gm5" class="delay"><a class="btn" href="#" id="next">Next</a></li><li id="gm4" class="delay"><a class="btn" href="#" id="prev">Previous</a></li><li id="gm3" class="delay"><a class="btn" href="#" id="stop">Stop</a></li><li id="gm2" class="delay"><a class="btn" href="#" id="play">Play</a></li></ul>');
		// the old switcharoo
		$('.movie').wait(2).then.animate({'height': '297px'},500).wait(1).then.append('<div class="media-small" id="media-small"><embed height="297" width="310" autostart="1" bgcolor="#f6f6f6" pluginspage="http://www.microsoft.com/Windows/MediaPlayer/" type="application/x-mplayer2" src="'+movie+'" style="display: block;"/></div>');
		$('#media-big').wait(2).then.html('<img src="'+dir+'/image1.jpg" />').animate({'width': '640px', 'height': '480px'},500);
		$('#loader2').wait(2).then.fadeOut();
		$.post('_sh_inc/process.php', { location: dir, width:640, height:480, data_action: 'getHousePhotos' },
   		function(data){
   			$('#media-big').wait(3.5).then.append(data);
			setTimeout(function(){loadSlides('#media-big'); boxSwitchBack(movie,id,dir);},3700);
			$('.delay').wait(3).then.slideDown(800);
   			});
		return false;
	});
}

function boxSwitchBack(movie,id,dir){
	$('#o-slider').append('<a href="#" id="slide-with" class="btn">Slider</a>').fadeIn();
	$('#slide-with').click(function(){
		// the put things back the way they were yo
		$('.delay').remove();
		$('#media-big').cycle('stop').animate({'width': '0px'},500).wait(2).then.remove();
		$('#slide-with').remove();
		$('#gallery-menu').remove();
		$('#media-small embed,#media-small').empty().remove();
		$('.movie').html('<div id="loader2" style="width:100px;height:100px; margin-left:auto; margin-right:auto; margin-top:45px;"><img src="2.0/_images/ajax-loader.gif" /></div>').animate({'height': '233px'},500);
		$('#gallery-menu-left').html('<li id="gm5" class="delay"><a class="btn" href="#" id="next">Next</a></li><li id="gm4" class="delay"><a class="btn" href="#" id="prev">Previous</a></li><li id="gm3" class="delay"><a class="btn" href="#" id="stop">Stop</a></li><li id="gm2" class="delay"><a class="btn" href="#" id="play">Play</a></li>');
		$('#gallery-image').animate({'height': '545px'},500);
		$('#gallery-image').wait(2).then.html('<div class="media-big" id="media-big"><embed height="545" width="640" autostart="1" bgcolor="#f6f6f6" pluginspage="http://www.microsoft.com/Windows/MediaPlayer/" type="application/x-mplayer2" src="'+movie+'" style="display: block;"/></div>').wait(2).then.animate({'height': '545px'},500);
		getHousePics(dir,310,233,'.movie');
		boxSwitch(movie,id,dir);
		$('.delay').wait(4).then.slideDown(800);
		return false;
	});
}

// function to add slides to the slideshow on the fly - for speed purposes
function loadSlides(div){
	buttonRoll();
	 
    // start slideshow
    $(div).cycle({
        		timeout:  6000,
        		prev:    '#prev',
        		next:    '#next',
        		speed:       1
	});
	$(div).cycle('pause');
     		$('#stop').click(function() { 
    			$(div).cycle('pause');
    			return false;
			});
			$('#play').click(function() { 
    			$(div).cycle('resume');
    			return false;
			});
}

// method for chaining and pausing jquery events
var ChainCollector = function(base) {
var CLASS = arguments.callee;
this.then = this.and = this;
var queue = [], baseObject = base || {};
this.____ = function(method, args) {
queue.push({func: method, args: args});
};
this.fire = function(base) {
var object = base || baseObject, method, property;
for (var i = 0, n = queue.length; i < n; i++) {
method = queue[i];
if (object instanceof CLASS) {
object.____(method.func, method.args);
continue;
}
property = object[method.func];
object = (typeof property == 'function')
? property.apply(object, method.args)
: property;
}
return object;
};
};
ChainCollector.addMethods = function(object) {
var methods = [], property, i, n, name;
var self = this.prototype;
var reservedNames = [], blank = new this();
for (property in blank) reservedNames.push(property);
var re = new RegExp('^(?:' + reservedNames.join('|') + ')$j');
for (property in object) {
if (Number(property) != property)
methods.push(property);
}
if (object instanceof Array) {
for (i = 0, n = object.length; i < n; i++) {
if (typeof object[i] == 'string')
methods.push(object[i]);
}
}
for (i = 0, n = methods.length ; i < n; i++)
(function(name) {
if (re.test(name)) return;
self[name] = function() {
this.____(name, arguments);
return this;
};
})(methods[i]);
if (object.prototype)
this.addMethods(object.prototype);
};
jQuery.fn.wait = function(time) {
var collector = new ChainCollector(), self = this;
// Deal with scoping issues...
var fire = function() { collector.fire(self); };
setTimeout(fire, Number(time) * 1000);
return collector;
};
// Then extend ChainCollector with all jQuery's methods
ChainCollector.addMethods(jQuery);


// grid overlay for devel
var gOverride = {
  urlBase: 'http://gridder.andreehansson.se/releases/latest/',
  gColor: '#333333',
  gColumns: 12,
  gOpacity: 0.35,
  gWidth: 10,
  pColor: '#C0C0C0',
  pHeight: 15,
  pOffset: 0,
  pOpacity: 0.55,
  center: true,
  gEnabled: true,
  pEnabled: true,
  setupEnabled: true,
  fixFlash: true,
  size: 960
};