var CD;

CD = {
	wrapper : '',
	footer : '',
	content : '',
	header : '',
	navigation : '',
	loading : '',
	
	init : function() {
		CD.navigation = jQuery("#nav");
		CD.wrapper = jQuery(".wrapper");
		CD.content = jQuery("#content");
		CD.footer  = jQuery("#footer");
		CD.header  = jQuery(".header");
		CD.loading = CD.content.find(".loading");

		CD.common.init();
	},
	loader : {
		hash : '',
		init : function(_url) {
		    if(_url.indexOf(".co.uk/") > 0 ) {
    			CD.loader.hash = _url.split(".co.uk/");
    	    } else if(_url.indexOf("/") == 0) {
                CD.loader.hash = _url.split("/");
    	    }
			CD.loader.hash = CD.loader.hash[1];

			if(CD.loader.hash.length == 0) {
				CD.loader.hash = "home"
			}
			CD.loader.hash = CD.loader.hash.split(".ht");
			CD.loader.hash = CD.loader.hash[0];
			if(CD.loader.hash.indexOf("/") > 0) {
				CD.loader.hash = CD.loader.hash.split("/");
				CD.loader.hash = CD.loader.hash[0];
			}
			
			location.hash = CD.loader.hash;

			CD.loader.removeOld();
		},
		removeOld : function() {
			CD.content.find('.page').remove();
			CD.loader.showLoading();
		},
		showLoading : function() {
			CD.loading.fadeIn(500, function() {
				CD.loader.load(CD.loader.hash);
			});
		},
		load : function(_url) {
			pageTracker._trackPageview('/ajax/'+ _url);
			jQuery.ajax({
				url: _url+"?forceAjax=1",
				cache: false,
				success: function(html){
					CD.loading.fadeOut(500, function() {
						jQuery(html).insertBefore(CD.content.find(".loading"));
						if(_url.indexOf('-') > 0) {
							_url = _url.replace(/-/g,"_");
						}
						if( eval("typeof CD."+_url + " != 'undefined'") ) {
							eval("CD."+_url+".init()");
						} else {
	                        CD.content.find('a[href]').click(function() {
                               CD.loader.init(this.href);
                               return false; 
                            });
						}
						CD.content.find('.page').fadeIn(750);
					});
				}
			});
		},
	},
	common : {
		init : function() {
			CD.common.setup_nav();
		},
		setup_nav : function() {
			CD.header.find('h1 > a').click(function() {
			    CD.navigation.find("li > a[href='/']").attr("class", "current");
				CD.loader.init(this.href);
				return false;
			});
			CD.navigation.find('a').click(function() {
				CD.navigation.find('li').attr("class", "link");
				jQuery(this).parent().attr("class", "current");

				CD.loader.init(this.href);
				return false;
			});
		}
	},
	home : {
		items : '',
		holder : '',
		items_count : '',
		button_prev : '',
		button_next : '',
        running : false,
		
		init : function() {
			CD.home.holder = jQuery('#showcase .items');
			CD.home.items = CD.home.holder.find(".item");
			CD.home.button_prev = jQuery('#showcase .prev');
			CD.home.button_next = jQuery('#showcase .next');
			CD.home.items_count = CD.home.items.length;
			
			CD.home.setupButtons();
		},
		setupButtons : function() {
			CD.home.button_prev.click(function() {
				if(!CD.home.button_prev.hasClass("disabled") && CD.home.running == false) {
				    CD.home.running = true;
					CD.home.button_next.removeClass("disabled");
					CD.home.holder.animate({"marginLeft" : "+=790"}, 600, 'easeOutQuart', function() {
				        CD.home.running = false;
						if(CD.home.holder.css("marginLeft") == "0px") {
							CD.home.button_prev.addClass("disabled");
						}
					});
				}

				return false;
			});
			CD.home.button_next.click(function() {                        
				if(!CD.home.button_next.hasClass("disabled") && CD.home.running == false) {
				    CD.home.running = true;
					CD.home.button_prev.removeClass("disabled");
					CD.home.holder.animate({"marginLeft" : "-=790"}, 600, 'easeOutQuart', function() {
				        CD.home.running = false;
						if(CD.home.holder.css("marginLeft") == "-"+((CD.home.items_count-1)*790)+"px") {
							CD.home.button_next.addClass("disabled");
						}
					});
				}
				
				return false;
			});
		}
	},
	services : {
		items : '',
		
		init : function() {
			CD.services.items = CD.content.find('#services > .item');
			
			CD.services.items.hover(function() {
				jQuery(this).addClass("hover");
			}, function() {
				jQuery(this).removeClass("hover");
			});
			
			CD.services.items.find('h2 > a').click(function() {
				CD.loader.init(this.href);
				
				return false;
			});
			CD.services.items.find('img').click(function() {
				CD.loader.init( jQuery(this).siblings('h2').find("a").attr("href") );
				
				return false;
			});
		}
	},
	portfolio : {
		nav : '',
		categories : '',
		items  : '',
		thumbs : '',
		viewer : '',
		
		init : function() {
			CD.portfolio.nav = jQuery('#portfolio-nav');
			CD.portfolio.categories = CD.portfolio.nav.find(".category");
			CD.portfolio.items = CD.portfolio.nav.find(".items");
			CD.portfolio.thumbs = CD.portfolio.nav.find('.item > .thumb');
			CD.portfolio.viewer = jQuery('#portfolio-viewer');

			CD.portfolio.accordion.init();
			CD.portfolio.setupViewer();
			CD.portfolio.setupThumbsHover();
		},
		setupViewer : function() {
			var item;
			CD.portfolio.items.find(".item").each(function() {
				item = jQuery(this);
				CD.portfolio.viewer.find(".items").append(
					'<div class="item">'
					+	'<img class="screenshot" src="'+item.find('.thumb').attr("href")+'" alt="'+item.find('.thumb > img').attr("alt")+'" width="590" height="330" />'
					+	'<div class="details">'
					+		item.find('.details').html()
					+	'</div>'
					+'</div>'
				);
			});
            CD.portfolio.viewer.find('a[href]').click(function() {
               CD.loader.init(this.href);
               return false; 
            });
		},
		setupThumbsHover : function() {
			CD.portfolio.thumbs.click(function() {
				var clicked = jQuery(this);
				CD.portfolio.viewerTo(clicked.attr("href"));
				CD.portfolio.thumbs.removeClass("active");
				clicked.addClass("active").css({
					"opacity": "1",
					"zIndex": 200
				});
				
				CD.portfolio.nav.find(".largethumb").remove();
				
				return false;
			}).hover(function() {
				if( CD.portfolio.nav.find(".largethumb").length == 0 ) {
					CD.portfolio.nav.append('<img class="largethumb" alt="" width="" height="" />');
				}
				var hovered = jQuery(this);
				var largethumb = CD.portfolio.nav.find(".largethumb");
				
				var largecss = {
					top : hovered.position().top - 18,
					left : hovered.position().left - 31,
					height : '60px',
					width  : '107px',
					display : 'block'
				};
				// 60 - 25 = 35 / 2 = 17.5
				// 107 - 45 = 62 / 2 = 31
				if((hovered.position().top  - 18) < 0) largecss.top = 0;
				if((hovered.position().left - 31) < 0) largecss.left = 0;
				if((hovered.position().top  + 25 + 18) >= CD.portfolio.nav.height()) largecss.top = (CD.portfolio.nav.height() - 60);
				if((hovered.position().left + 45 + 31) >= CD.portfolio.nav.width()) largecss.left = (CD.portfolio.nav.width() - 107);
				
				largethumb.attr({
					src : hovered.find("img").attr("src"),
					width   : '161',
					height  : '90',
					display : 'none'
				}).css(largecss);
				
				hovered.css({
					opacity : 0,
					zIndex : 205
				});
				
				// think i should make the hoverer transparent and infront of the largethumb, and have the hover out remove on that, rather
				// than on the large thumb				
				largethumb
					.click(function() {
						CD.portfolio.viewerTo(hovered.attr('href'));
						
						return false;
					})
					.ready(function() {
						jQuery(this).css("display", "block")
					});
			}, function() {
				// hover out.. do nothing
				jQuery(this).css({
					"opacity": "1",
					"zIndex": 200
				});
				CD.portfolio.nav.find(".largethumb").remove();
			});
		},
		viewerTo : function(_url) {
			var assetsURL = _url.split('assets/');
			assetsURL = assetsURL[1];
			var largeImage = CD.portfolio.viewer.find('.item > img[src$='+assetsURL+']');
			CD.portfolio.viewer.find('.items').animate({'marginTop':"-"+largeImage.parent().position().top}, 1000)
		},
		accordion : {
			init : function() {
				// set the first one active
				jQuery(CD.portfolio.categories[0]).addClass("active");
				CD.portfolio.categories.find("h2 > a").click(function() {
					CD.portfolio.accordion.changeTo( jQuery(this).parent().attr("id") );

					return false;
				});
			},
			changeTo : function(_to) {
				CD.portfolio.categories.removeClass("active");
				CD.portfolio.categories.find("#"+_to).parent().addClass("active");
				CD.portfolio.categories.filter(".active").find(".items")
					.animate({
						height : 0
					}, 500)
					.css({'display':'none'})
					.parent().removeClass("active");

				var newActive = CD.portfolio.categories.find('#'+_to).siblings(".items");
				newActive.animate({
						height : newActive.height()
					}, 500).addClass("active");
			},
			openNew : function() {

			},
			closeOld : function() {

			}
		}
	},
    contact : {
        init : function() {
            jQuery("#Form_Form").validate({
                errorClass: "required",
                messages: {
                    EditableTextField1:"'Name' is required.",
                    EditableEmailField4:"'Email' is required.",
                    EditableTextField5:"'Message' is required."
                },
                rules: {
                    EditableTextField1:{
                        required:1, 
                        maxlength:"32"
                    },
                    EditableTextField2:{
                        maxlength:"32"
                    },
                    EditableEmailField4:{
                        required:1,
                        email:1
                    },
                    EditableTextField3:{
                        maxlength:"32"
                    },
                    EditableTextField5:{
                        required:1,
                        maxlength:"32"
                    }
                }
            });
        }
    }
};