(function($) { 

	$.generic = {
		navbg: null,
		navopacity: null,
		init: function(navbg,navopacity){
			this.binds();
			$.generic.navbg = navbg;
			$.generic.navopacity = navopacity;
			this.navigation();
		},
		navigation: function(){
			var filter = 'alpha(opacity=' + $.generic.navopacity*100 + ')'
			$("#navigation").transBGdraw({'background': $.generic.navbg, 'opacity': $.generic.navopacity, '-moz-opacity': $.generic.navopacity,'filter': filter });
		},
		binds: function(){
			$(".scrollleft").live('click',function(e){
				e.preventDefault();
				$(window).scrollTo({top:0,left:0},800);
			});
			
			$(".contact_form #submit").attr('disabled','');
			$(".contact_form").submit(function(){

				$(".contact_form .send_status").html("");
				$(".contact_form .send_status").removeClass('failed').removeClass('success');
				
				var errors = 0;
				$('.contact_form #name').css('background','transparent');
				if($('.contact_form #name').val() == "") {
					$('.contact_form #name').css('background','#eaa0ac');
					$(".contact_form .send_status").html("<img src=\"/application/views/default/images/delete.png\" alt=\"\"/ style=\"padding-right:10px;\">Please complete the mandatory fields.").addClass('failed');
					errors++;
				}
				if(errors > 0) { return false; }
				
				$(".contact_form .spinner").show();
				
				var submitval = $(".contact_form #submit").val();
				$(".contact_form #submit").val("Sending...");
				
				var postlocation = $(".contact_form").attr('action');

				var data = "toemail=" + $('.contact_form #toemail').val()  + "&submit=1&name=" + $('.contact_form #name').val() + "&email=" + $('.contact_form #email').val() + "&telephone=" + $('.contact_form #telephone').val() + "&message=" + $('.contact_form #message').val() + "&company=" + $('.contact_form #company').val() + "&website=" + $('.contact_form #website').val();
				
				
				$.ajax({
					type: "POST",
					url: "/page/contact/",
					data: data,
					success: function(response){
						$(".contact_form .spinner").hide();
						if(response == "1") {
							$(".contact_form .send_status").html("<img src=\"/application/views/default/images/green_tick.png\" alt=\"\"/ style=\"padding-right:10px;\">Your feedback was sent successfully.").addClass('success');
							$(".contact_form #submit").val("Sent");
							$(".contact_form #submit").attr('disabled','disabled');
						}
						else {
							$(".contact_form .send_status").html("<img src=\"/application/views/default/images/delete.png\" alt=\"\"/ style=\"padding-right:10px;\">Sending feedback failed.").addClass('failed');
							$(".contact_form #submit").val(submitval);
						}

						return false;
					},
					error: function(response) {
						$(".contact_form .spinner").hide();
						$(".contact_form .send_status").html("<img src=\"/application/views/default/images/delete.png\" alt=\"\"/ style=\"padding-right:10px;\">Sending feedback failed.").addClass('failed');
						
						$(".contact_form #submit").val(submitval);
						return false;
					}
				 });
				return false;

			});
		},
		collapsablenav: function() {
			var pathArray = window.location.pathname;
			var url = "";
			for (var i = 0; i < pathArray.length; i++) {
				url += pathArray[i];
			}

			$(".subnav").each(function(){
				
				$(".subnav").parent().find('a:first').addClass('arrowdown');	
				var urlmatch = 0;
		

				if(url != "/") {
					$(this).find('li').each(function(){
						var currLink = $(this).find('a');
						if(currLink.attr('href')) {
							if(currLink.attr('href').indexOf(url) > -1) {
								urlmatch = 1;
							}
						}

					});
				}
					
				if(urlmatch == 0) {
					$(this).hide();
				}

				$(this).parent().find('a:first').bind('click',function(e) {
					e.preventDefault();
					$(this).parent().find('.subnav').slideToggle(400, function(){
						var filter = 'alpha(opacity=' + $.generic.navopacity*100 + ')'
						$("#navigation").transBGdraw({'background': $.generic.navbg, 'opacity': $.generic.navopacity, '-moz-opacity': $.generic.navopacity,'filter': filter });
					});

				});

			});

		}

	},

	$.rectangle_layout = {
		init: function(){
			Shadowbox.init({
			   troubleElements: ["select"]
			});
			
			$(".rectangle_layout ul li img").each(function(){
				if($(this).width() > $(this).parent().parent().width()) {
					var imgwidth = $(this).width();
					var boxwidth = $(this).parent().parent().width();
					
					var offset = (imgwidth - boxwidth) / 2;
					
					$(this).css('margin-left',"-"+offset+'px');
				}
			});
		}
	},
	$.smallsquare_layout = {
		init: function(){
			Shadowbox.init({
			   displayCounter: false,
			   troubleElements: ["select"]
			});
		}
	},
	$.vertlargeimg_layout = {
		init: function(){
			
		}
	}
	$.largeimg_layout = {

		init: function(){
			var totalwidths = 0;
			$(".largeimg_layout img").each(function(){
				totalwidths += $(this).width() + 20;
			});
			$(".largeimg_layout,.largeimg_layout ul").css('width',totalwidths);

			var mousewheelevt=(/Firefox/i.test(navigator.userAgent)) ? "DOMMouseScroll" : "mousewheel";
			if (document.attachEvent) 

			document.attachEvent("on"+mousewheelevt, $.largeimg_layout.scrollwheel);  

			else if (document.addEventListener)
			  document.addEventListener(mousewheelevt, $.largeimg_layout.scrollwheel, false);
		},
		scrollwheel: function(event) {
			  var delta = event.detail ? event.detail * (-120) : event.wheelDelta
			  var scrollOffset = 30 * (delta / -120);

			  window.scrollBy(scrollOffset, 0);

			  event.preventDefault;
			  event.stopPropagation;
		}

	},

	$.fadenav_layout = {
		imagecount: 0,
		current: 1,
		init: function(){
			$.fadenav_layout.fadecontrols();
		},
		fadecontrols: function(){
			$('#fadenav_images').exposure({controlsTarget : '.fadenav_layout #controls', 
				showThumbnails : false,
				preloadBuffer: 5,
				onImage : function(image, imageData, thumb) {

						var hovered = $('.fadenav_layout .exposureWrapper').hasClass('exposureHover');

						// Fade out the previous image.
						$('.fadenav_layout .exposureWrapper > .fadenav_layout .exposureLastImage').stop().fadeOut(500, function() {
							$(this).remove();
						});

						// Fade in the current image.
						image.hide().stop().fadeIn(1000);

						var hasCaption = function() {
							return imageData.find('.caption').html().length > 0 || imageData.find('.extra').html().length > 0;
						}

						var showImageData = function() {
							imageData.stop().show().animate({bottom:0+'px'},{queue:false,duration:160});
						}

						var hoverOver = function() {
							$('.fadenav_layout .exposureWrapper').addClass('exposureHover');
							// Show image data as an overlay when image is hovered.

							var hasCpt = hasCaption();

							if (hasCpt) {
								showImageData.call();
							}
						};

						

						var hideImageData = function() {
							var imageDataBottom = -imageData.outerHeight();
							imageData.stop().show().animate({bottom:imageDataBottom+'px'},{queue:false,duration:160});
						}

						var hoverOut = function() { 
							$('.fadenav_layout .exposureWrapper').removeClass('exposureHover');
							// Hide image data on hover exit.
							if (hasCaption()) {
								hideImageData.call();
							}
						};

						$('.fadenav_layout .exposureWrapper').hover(hoverOver,hoverOut);
						imageData.hover(hoverOver,hoverOut);
										
						if (hovered) {
							if (hasCaption()) {
								showImageData.call();
							} else {
								hideImageData.call();	
							}	
						}
				}

			});

		}

	},

	$.smallthumb_layout = {
		init: function(){
			$.smallthumb_layout.hookthumbs();
			$.smallthumb_layout.slider()
		},
		hookthumbs: function(){
				$('#smallthumb_images').exposure({controlsTarget : '.smallthumb_layout #controls',
					controls : { prevNext : false, pageNumbers : false, firstLast : false },
					pageSize : 2000,
					preloadBuffer: 5,
					onThumb : function(thumb) {
						var li = thumb.parents('li');				
						var fadeTo = li.hasClass('active') ? 1 : 0.3;
						thumb.css({display : 'none', opacity : fadeTo}).stop().fadeIn(200);

						thumb.hover(function() { 
							thumb.fadeTo('fast',1); 
						}, function() { 
							li.not('.active').children('img').fadeTo('fast', 0.3); 
						});
					},

					onImage : function(image, imageData, thumb) {

						// Check if wrapped is hovered.
						var hovered = $('.smallthumb_layout .exposureWrapper').hasClass('exposureHover');

						

						// Fade out the previous image.
						$('.smallthumb_layout .exposureWrapper > .smallthumb_layout .exposureLastImage').stop().fadeOut(500, function() {
							$(this).remove();
						});

						

						// Fade in the current image.

						image.hide().stop().fadeIn(1000);

						var hasCaption = function() {
							return imageData.find('.caption').html().length > 0 || imageData.find('.extra').html().length > 0;
						}

			
						var showImageData = function() {
							imageData.stop().show().animate({bottom:0+'px'},{queue:false,duration:160});
						}

						var hoverOver = function() {
							$('.smallthumb_layout .exposureWrapper').addClass('exposureHover');

							// Show image data as an overlay when image is hovered.
							var hasCpt = hasCaption();
						
							if (hasCpt) {
								showImageData.call();
							}
						};

						

						var hideImageData = function() {
							var imageDataBottom = -imageData.outerHeight();
							imageData.stop().show().animate({bottom:imageDataBottom+'px'},{queue:false,duration:160});
						}

						var hoverOut = function() { 
							$('.smallthumb_layout .exposureWrapper').removeClass('exposureHover');
							// Hide image data on hover exit.
							if (hasCaption()) {
								hideImageData.call();
							}
						};

						

						$('.smallthumb_layout .exposureWrapper').hover(hoverOver,hoverOut);
						imageData.hover(hoverOver,hoverOut);

												
						if (hovered) {
							if (hasCaption()) {
								showImageData.call();
							} else {
								hideImageData.call();	
							}	
						}

		

						if ($.exposure.showThumbs && thumb && thumb.length) {
							thumb.parents('li').siblings().children('img.selected').stop().fadeTo(200, 0.7);			
							thumb.fadeTo('fast', 1).addClass('selected');
						}

					}

				});

		},
		slider: function(){
			var step = 9; 
			var current = 0; 
			var maximum = $('#small_thumbs_container ul li').size(); 
			var visible = 10; 
			var speed = 800; 
			var liSize = 74;
			var carousel_height = 64;
	 
	 
			var ulSize = liSize * maximum;   
			$('#small_thumbs_container ul').css("width", ulSize+"px").css("left", -(current * liSize)).css("position", "absolute");
			$('#small_thumbs_container').css("height", carousel_height+"px").css("visibility", "visible").css("overflow", "hidden").css("position", "relative"); 
	 
			$('.thumbsnext').click(function() { 
				if(current + step < 0 || current + step > maximum ) {return; }
				else {
					current = current + step;
					$('#small_thumbs_container ul').animate({left: -(liSize * current)}, speed, null);
				}
				return false;
			});
	 
			$('.thumbsprev').click(function() { 
				if(current - step < 0 || current - step > maximum - visible) {return; }
				else {
					current = current - step;
					$('#small_thumbs_container ul').animate({left: -(liSize * current)}, speed, null);
				}
				return false;
			});
			
			if($('#small_thumbs_container ul li').length <= visible) {
				$(".thumbsprev").fadeOut();
				$(".thumbsnext").fadeOut();
			}
			
		}

		

	},

	$.portrait_layout = {

		COLNUMBER : 2, // Minimum column number.
		COLMARGIN : 10, // Margin (in pixel) between columns/boxes.
		COLWIDTH : 254, // Fixed width of all columns.
		init: function(){

			//S$.portrait_layout.buildlayout();


			$(window).load(
			    function() {
				$(window).resize(function() {
					$.portrait_layout.buildlayout();
				});
				$.portrait_layout.buildlayout();

			    }
			);
			Shadowbox.init({
				troubleElements: ["select"]
			});

		},
		buildlayout: function(){

			var pointer = 0;
			var arr = [];
			var columns = Math.max($.portrait_layout.COLNUMBER, parseInt($('.portrait_layout').innerWidth() / ($.portrait_layout.COLWIDTH + $.portrait_layout.COLMARGIN)));

			$('.portrait_layout .box').css('position', 'absolute').css('width', $.portrait_layout.COLWIDTH  + 'px');
			
			$('.portrait_layout .box').each(function() {
				var tempLeft = (pointer * ($.portrait_layout.COLWIDTH + $.portrait_layout.COLMARGIN));
				$(this).css('left', tempLeft + 'px');
				var tempTop = 0;
				if (arr[pointer]) { tempTop = arr[pointer];	}
				$(this).css('top', tempTop + 'px');

				arr[pointer] = tempTop + $(this).outerHeight() + $.portrait_layout.COLMARGIN;
				pointer++;
				if (pointer === columns) { pointer = 0; }
			});

			
		}

	}

})(jQuery);


