$j(document).ready(function() {
	// Preload CSS Images
	$j.preloadCssImages();
	
	//box_links_spread.init();
	//btns_spread.init();
	reduce.init();
	
	// General page layout
	content_boxes_height_even.init();
	columns_even.init();
	
	// FAQs
	faq.init();
	
	// Galleries
	gallery.init();
	
	// Slideshows
	slideshow.init();
	
	// Overlay
	overlay.init();
	
	// Panorama
	//panorama.init();
});

box_links_spread = {
	init : function() {
		width_available = 192;
		min_width = 96;
		$j("#boxes div.links").each(function (i) {
			var width_used = 0;
			var kids = $j(this).children();
			var uneven = false;
			kids.each(function(j) {
				width_used += $j(this).width();
				if($j(this).width() > min_width)
					uneven = true;
			});
			if (uneven) {
				kids.each(function(j){
					if ($j(this).width() < min_width) {
						//$j(this).width($j(this).width() + width_available - width_used);
						$j(this).find('span').width($j(this).find('span').width() + width_available - width_used);
					}
				});
			} else {
				kids.each(function(j){
					difference = min_width - $j(this).width();
					$j(this).find('span').width($j(this).find('span').width()+difference);
					//$j(this).width(min_width);	
				});
			}
		});
	}
}

home_box_links_spread = {
	init : function() {
		width_available = 192;
		min_width = 95;
		$j(".box div.links").each(function (i) {
			var width_used = 0;
			var kids = $j(this).children();
			var uneven = false;
			kids.each(function(j) {
				width_used += $j(this).width();
				if($j(this).width() > min_width)
					uneven = true;
			});
			if (uneven) {
				kids.each(function(j){
					if ($j(this).width() < min_width) {
						//$j(this).width($j(this).width() + width_available - width_used);
						$j(this).find('span').width($j(this).find('span').width() + width_available - width_used);
					}
				});
			} else {
				kids.each(function(j){
					difference = min_width - $j(this).width();
					$j(this).find('span').width($j(this).find('span').width()+difference);
					//$j(this).width(min_width);
				});
			}
		});
	}
}

content_box_links_spread = {
	init : function() {
		$j('#content .boxpair .inside').each(function(){
			var total_width = $j(this).width();
			var half = total_width / 2;
			var uneven = false;
			var width_used = 0;
			var amount = 0;
			$j(this).find('a.linkbtn').each(function(){
				width_used += $j(this).outerWidth();
				if($j(this).width() > half) uneven = true;
				amount++;
			});
			var available_width = total_width - width_used;
			if(uneven) {
				$j(this).find('a.linkbtn').each(function(){
					if($j(this).width() < half) {
						$j(this).find('span').width($j(this).find('span').width() + available_width);
					}
				});
			} else {
				if(amount > 1) {
					$j(this).find('a.linkbtn').each(function(){
						$j(this).find('span').width($j(this).find('span').width() + available_width / 2);
					});
				} else {
					$j(this).find('a.linkbtn').each(function(){
						span_width = $j(this).find('span').width();
						link_width = $j(this).outerWidth();
						difference = link_width - span_width;
						$j(this).find('span').width(half - difference);
					});
				}
			}
		});
	}
}

expandable_box_links_spread = {
	init : function() {
		width_available = 279;
		min_width = Math.floor(width_available/2);
		$j(".expandable div.links").each(function (i) {
			var width_used = 0;
			var kids = $j(this).children();
			var uneven = false;
			kids.each(function(j) {
				width_used += $j(this).width();
				if($j(this).width() > min_width)
					uneven = true;
			});
			if (uneven) {
				kids.each(function(j){
					if ($j(this).width() < min_width) 
						//$j(this).width($j(this).width() + width_available - width_used);
						$j(this).find('span').width($j(this).find('span').width() + width_available - width_used);
				});
			} else {
				kids.each(function(j){
					difference = min_width - $j(this).outerWidth();
					$j(this).find('span').width($j(this).find('span').width()+difference);
				});
			}
		});
	}
}

btns_spread = {
	init : function() {
		min_width = 96;
		$j(".btn").each(function (i) {
			if ($j(this).width() < min_width) {
				//$j(this).width(min_width);
				difference = min_width - $j(this).width();
				$j(this).find('span').width($j(this).find('span').width() + difference);
			}
		});
		$j("button").each(function (i) {
			if ($j(this).width() < min_width) {
				//$j(this).width(min_width);
				difference = min_width - $j(this).width();
				$j(this).width($j(this).width() + difference);
			}
		});
	}
}

reduce = {
	init : function() {
		number_reduce = 0;
		number_id = 0;
		reduce_by = 224;
		max_height = 0;
		subsequent = false;
		shadow_dis = 2;
		$j(".item.reduce").each(function (i) {
			var item = $j(this).find(".left");
			// Height
			if ($j(this).find('.left').height() > max_height) 
				max_height = $j(item).height();
			boxes = $j(this).find(".box");
			if(!item.hasClass(".first")) {
				boxes_height = 0
				for (i = 0; i < boxes.length; i++) {
					boxes_height += $j(boxes[i]).height();
				}
				if(max_height > boxes_height) {
					for (i = 0; i < boxes.length; i++) {
						$j(boxes[i]).height(max_height/boxes.length - shadow_dis);
					}
				} else {
					$j(item).height(boxes_height + (boxes.length*shadow_dis));
				}
			} else {
				for (i = 0; i < boxes.length; i++) {
					if ($j(boxes[i]).height() > max_height) 
						max_height = $j(boxes[i]).height();
				}
				if(max_height > $j(item).height())
					$j(item).height(max_height);
				else
					$j(item).height(max_height);
				for (i = 0; i < boxes.length; i++) {
					$j(boxes[i]).height(max_height);
				}
			}
		});
		home_box_links_spread.init();
		content_box_links_spread.init();
		expandable_box_links_spread.init();
	}
}

content_boxes_height_even = {
	init : function() {
		$j('#content .boxpair').each(function (i) {
			max_height = 0;
			$j(this).find('.inside').each(function(j) {
				if($j(this).height() > max_height) max_height = $j(this).height();
			});
			$j(this).find('.inside').each(function(j) {
				$j(this).height(max_height);
			});
		});
	}
}

columns_even = {
	init : function() {
		var content = $j('#content').height();
		
		top_heights = 0;
		$j('#box_holder .bt').each(function (i) {
			top_heights += $j(this).height();
		});
		bottom_heights = 0;
		$j('#box_holder .bb').each(function (i) {
			bottom_heights += $j(this).height();
		});
		
		if ($j("#box_holder").length > 0){
			boxes = $j('#box_holder .boxes').height() - top_heights - bottom_heights;
			box_padding = $j('#box_holder .boxes').outerHeight() - $j('#box_holder .boxes').height(); 
		
			final_content = content - top_heights - bottom_heights - box_padding;
			if(final_content > boxes) {
				$j('#box_holder .boxes').height(content - top_heights - bottom_heights - box_padding);
			}
		}
	}
}

gallery = {
	init : function() {
		$j('#content .gallery .image_holder').each(function (i) {
			$j(this).clone().appendTo($j(this).parent()).addClass('clone');
			$j(this).clone().appendTo($j(this).parent()).addClass('clone');
			$j(this).addClass('original');
			$j(this).parent().width($j(this).width()*3);
		});
		$j('#content .gallery .image_bar_wrapper a.previous').each(function (i) {
			gallery.move(this, '+');
		});
		$j('#content .gallery .image_bar_wrapper a.next').each(function (i) {
			gallery.move(this, '-');
		});
	},
	move : function(that, direction) {
		$j(that).click(function(event) {
			event.preventDefault();
			var holder_width = 522;
			gallery.check(that, direction, holder_width);
			$j(that).parent().find('.images_wrapper').animate({
				left: direction+'='+holder_width/*$j(this).parent().find('.images').width()*/
			}, 500, function() {
				
			});
		});
	},
	check : function(that, direction, holder_width) {
		if(direction == '+') {
			if($j(that).parent().find('.images_wrapper').position().left > -(holder_width)) {
				$j(that).parent().find('.images_wrapper').css('left', $j(that).parent().find('.images_wrapper').position().left - $j(that).parent().find('.images_wrapper').width() / 3 + 'px');
			}
		} else {
			if($j(that).parent().find('.images_wrapper').position().left + $j(that).parent().find('.images_wrapper').width() - (holder_width * 3) < 0) {
				$j(that).parent().find('.images_wrapper').css('left', $j(that).parent().find('.images_wrapper').position().left + $j(that).parent().find('.images_wrapper').width() / 3 + 'px');
			}
		}
	}
}

var max_slideshow_image_width = 0;
var interval_id;
slideshow = {
	init : function() {
		$j('#content .slideshow_holder .slideshow .slideshow_categories').each(function(){
			$j(this).clone().appendTo($j(this).parent()).addClass('clone');
			$j(this).clone().appendTo($j(this).parent()).addClass('clone');
			$j(this).addClass('original');
		});
		slideshow.calculate_width();
		$j('#content .slideshow_holder .categories_bar .radio input').click(function(event) {
			var chosen_category = $j(this).attr('value');
			var found = false;
			$j(this).closest('.slideshow_holder').find('.slideshow_categories .slideshow_photos').each(function(){
				if($j(this).hasClass('category_'+chosen_category)) {
					$j(this).show();
					found = true;
				} else {
					$j(this).hide();
				}
			});
			if(!found) {
				$j(this).closest('.slideshow_holder').find('.slideshow_categories .slideshow_photos').each(function(){
					$j(this).show();
				});
			}
			$j(this).closest('.slideshow_holder').find('.slideshow').css('left', 0);
			slideshow.calculate_width();
		});
		$j('#content .slideshow_holder .navigation_bar .left a').each(function (i) {
			$j(this).click(function(event){
				event.preventDefault();
				var direction = $j(this).attr('class');
				direction = (direction == 'previous') ? '+' : '-'; 
				slideshow.stop();
				slideshow.move(this, direction);
			});
		});
		$j('#content .slideshow_holder .navigation_bar .right a.play_slideshow').each(function(i) {
			$j(this).click(function(event){
				event.preventDefault();
				slideshow.start(this);
			});
		});
	},
	calculate_width : function() {
		$j('#content .slideshow_holder .slideshow .slideshow_categories').each(function(){
			var total_width = 0;
			$j(this).find('img:visible').each(function(){
				if(max_slideshow_image_width < $j(this).parent().width()) max_slideshow_image_width = $j(this).parent().width();
				total_width += parseInt($j(this).parent().width());
			});
			$j(this).parent().width(total_width*3);
		});
	},
	move : function(that, direction) {
		slideshow.check(that, direction);
		$j(that).closest('.slideshow_holder').find('.slideshow').animate({
			left: direction+'='+max_slideshow_image_width
		}, 500, function() {
			
		});
	},
	check : function(that, direction) {
		if(direction == '+') {
			if($j(that).closest('.slideshow_holder').find('.slideshow').position().left > -(max_slideshow_image_width)) {
				$j(that).closest('.slideshow_holder').find('.slideshow').css('left', $j(that).closest('.slideshow_holder').find('.slideshow').position().left - $j(that).closest('.slideshow_holder').find('.slideshow_categories').width() + 'px');
			}
		} else {
			if($j(that).closest('.slideshow_holder').find('.slideshow').position().left + $j(that).closest('.slideshow_holder').find('.slideshow').width() - (max_slideshow_image_width * 3) < 0) {
				$j(that).closest('.slideshow_holder').find('.slideshow').css('left', $j(that).closest('.slideshow_holder').find('.slideshow').position().left + $j(that).closest('.slideshow_holder').find('.slideshow_categories').width() + 'px');
			}
		}
	},
	start : function(that) {
		slideshow.stop();
		slideshow.move(that, '-');
		interval_id = setInterval(function() { slideshow.move(that, '-'); }, 2500);
	},
	stop : function() {
		clearInterval(interval_id);
	}
}

overlay = {
	init : function() {
		$j(".zoom").click(function (e) {
			e.preventDefault();
			id = $j(this).attr('rel');
			$j('#overlay_box #'+id).removeClass('none');
			showBox();
		});
	
		$j(".close").click(function (e) {
			e.preventDefault();
			hideBox();
			id = $j(this).attr('rel');
			$j('#overlay_box #'+id).addClass('none');
		});
	}
}


var percent = new Object;
var prevent_animation = false;
panorama = {
	init : function() {
		
		var disposition = -($j('#panorama .inside').width()/2 - $j('#panorama').width()/2);
		$j('#panorama .inside').css({left: disposition+'px'});
		
		$j().mousemove(function(e){
			
			mouse = new Object;
			mouse.x = e.pageX;
			mouse.y = e.pageY;
			
			window_size = window.get_size();
			
			window_percent = new Object;
			window_percent.x = window_size.x/100;
			window_percent.y = window_size.y/100;
			
			current_percent = new Object;
			current_percent.x = mouse.x/100;
			current_percent.y = mouse.y/100;
			
			percent.x = (current_percent.x/window_percent.x)*100;
			percent.y = (current_percent.y/window_percent.y)*100;
			//console.log(percent.x +', '+ percent.y);
		});
		
		interval = window.setInterval('animate()', 40);
		interval_state = true;
	}
}

function get_size() {
	var myWidth = 0, myHeight = 0;
	if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;
	}
	obj = new Object;
	obj.x = myWidth;
	obj.y = myHeight;
	return obj;
}

var speed_factor = 0.25;
function animate(){
	if (!prevent_animation) {
		if (!window.speed_factor) {
			speed_factor = 1;
		}
		disposition = (window.percent.x - 50) * speed_factor;//throttle_factor;
		if(isNaN(disposition)) disposition = 0;
		position = $j('#panorama .inside').position();
		if (position.left == 'undefined') 
			position.left = 0;
		
		current = position.left;
		new_pos = current - disposition;
		
		if(new_pos > 0) {
			new_pos = 0;
		} else {
			var max = $j('#panorama .inside img').width() - $j('#panorama').width();
			if(Math.abs(new_pos) > max)
				new_pos = -max;
		}
		
		$j('#panorama .inside').css("left", new_pos + "px");
	}
}

faq = {
	init : function() {
		$j(".faq .question").each(function(i){
			$j(this).closest('.faq').find('div[class*=answer]').removeClass('none').hide();
			
			$j(this).click(function(event){
				event.preventDefault();
				$j(".faq").find('div[class*=answer]:visible').slideUp();
				if($j(this).closest('.faq').find('div[class*=answer]:hidden')) {
					$j(this).closest('.faq').find('div[class*=answer]').slideDown();
				}
			});
		});
	}
}
