var cd = {
	init: function() {
		// create loading layer
		//if($('#loading').length < 1) {
		//	$('<div id="loading" />').css("opacity", 0).appendTo($('#main'));
		//}
		
		// page ajax script
		//$('#header nav a').cd_pageajax({
		//	callback: function() { cd.pagetypes.init(); }
		//});
		
		cd.bgimage.init();
		cd.pagetypes.init();
		
		Modernizr.load({
			test: ($.browser.msie && $.browser.version < 7),
			yep: "themes/default/js/libs/dd_belatedpng.js",
			callback: function(a, b) {
				if(b == false) return;
				DD_belatedPNG.fix('.png_bg');
			}
		});
	}, //init
	bgimage: {
		holder: null,
		timer: null,
		ajaxbase: "",
		raw_image_size: {
			w: 0,
			h: 0,
			ratio: 0
		},
		max_image_size: {
			w: 0,
			h: 0,
			ratio: 0
		},
		init: function() {
			cd.bgimage.holder = $('#photo');
			if(cd.bgimage.holder.length) {
				cd.bgimage.resize();
				$(window).resize(function() { 
					cd.bgimage.resize(); 
				});
				
				cd.bgimage.show();
				
				if($('body').hasClass("HomePage")) cd.bgimage.ajaxbase = "home/";
				else cd.bgimage.ajaxbase = $('#header nav a.current').attr("href");
				
				if(cd.bgimage.holder.attr("data-slideshow") == "1") {
					cd.bgimage.resettimer();
				}
				
			}
		},
		resize: function($img) {
			$img = $img || cd.bgimage.holder.children('img.current');
			cd.bgimage.raw_image_size.w = $img.attr("width");
			cd.bgimage.raw_image_size.h = $img.attr("height");
			cd.bgimage.raw_image_size.ratio = cd.bgimage.raw_image_size.w / cd.bgimage.raw_image_size.h;
			//console.log("raw_image_size; w=", cd.bgimage.raw_image_size.w, "h=", cd.bgimage.raw_image_size.h);
			
			var $win = $(window);
			cd.bgimage.max_image_size.w = $win.width();
			if($('body').hasClass("ProjectPage") || $('body').hasClass("SlideshowPage")) {
				cd.bgimage.max_image_size.w = cd.bgimage.max_image_size.w - 245; //width of header
			}
			cd.bgimage.max_image_size.h = $win.height();
			if(cd.bgimage.max_image_size.h < 850) cd.bgimage.max_image_size.h = 850;
			cd.bgimage.max_image_size.ratio = cd.bgimage.max_image_size.w / cd.bgimage.max_image_size.h;
			//console.log("max_image_size; w=", cd.bgimage.max_image_size.w, "h=", cd.bgimage.max_image_size.h);
			$win = null;
			
			//console.log("raw img ratio", cd.bgimage.raw_image_size.ratio);
			//console.log("max img ratio", cd.bgimage.max_image_size.ratio);
			
			// if raw image is smaller than both max sizes just display that
			if(cd.bgimage.raw_image_size.w <= cd.bgimage.max_image_size.w
			&& cd.bgimage.raw_image_size.h <= cd.bgimage.max_image_size.h) {
				//console.log("Let's just display the raw image :)");
			} else {
				if(cd.bgimage.raw_image_size.w > cd.bgimage.max_image_size.w
				&& cd.bgimage.raw_image_size.h <= cd.bgimage.max_image_size.h) {
					//console.log("width too big, height ok --- resize width");
					$img.css({
						width:cd.bgimage.max_image_size.w,
						height:'auto'
					});
				} else if(cd.bgimage.raw_image_size.w <= cd.bgimage.max_image_size.w
				&& cd.bgimage.raw_image_size.h > cd.bgimage.max_image_size.h) {
					//console.log("height too big, width ok --- resize height");
					$img.css({
						height:cd.bgimage.max_image_size.h,
						width:'auto'
					});
				} else {
					//console.log("both height and width are too big, decide which one to resize");
					if(cd.bgimage.raw_image_size.ratio > cd.bgimage.max_image_size.ratio) {
						//console.log("raw ratio > max ratio");
						$img.css({
							width:cd.bgimage.max_image_size.w,
							height:'auto'
						});
					} else {
						//console.log("raw ratio < max ratio");
						$img.css({
							height:cd.bgimage.max_image_size.h,
							width:'auto'
						});
					}
				}
			}
		},
		resettimer: function() {
			cd.bgimage.timer = setTimeout(function() { cd.bgimage.next(); }, 5000);
		},
		show: function($img) {
			$img = $img || cd.bgimage.holder.children('img.current');
			$img.siblings('.current').fadeOut(500, function() {
				$(this).remove();
			});
			$img.delay(500).fadeIn(1000).removeClass('next').addClass('current');
		},
		next: function() {
			cd.bgimage.ajax("getNextPhoto");
		},
		prev: function() {
			cd.bgimage.ajax("getPrevPhoto");
		},
		ajax: function(path) {
			//console.log("cd.bgimage.ajax", path);
			$.ajax({
				url: cd.bgimage.ajaxbase+path+"/?notid="+cd.bgimage.holder.children('.current').attr("data-classid"),
				success: function(data) {
					//console.log(data);
					var $img = $(data);
					$img.addClass('next').appendTo(cd.bgimage.holder);
					$img.load(function() {
						cd.bgimage.resize($img);
						cd.bgimage.show($img);
						cd.bgimage.resettimer();
					});
				}
			});
		}
	},
	pagetypes: {
		init: function() {			
			// setup formlabel
			//$('input.text, input.email, input.password, textarea, .dropdown select').cd_formlabel();
			
			switch($('body').attr("class")) {
				case 'HomePage': cd.pagetypes.HomePage.init(); break;
				case 'ProjectPage': cd.pagetypes.ProjectPage.init(); break;
				case 'ServicesHolderv2': cd.pagetypes.ServicesHolderv2.init(); break;
			}
		},
		HomePage: {
			init: function() {
			}
		},
		ProjectPage: {
			init: function() {
				// create show/hide
				var showhide_button = $('<a id="showhide_text" data-currently="showing" href="#">Hide Text</a>')
					.click(function(e) {
						e.preventDefault();
						if($(this).attr("data-currently") == "showing") {
							$(this).attr("data-currently", "hidden")
								.text("Show Text");
							$('#content .content-box').hide();
						} else {
							$(this).attr("data-currently", "showing")
								.text("Hide Text");
							$('#content .content-box').show();
						}
						return false;
					});
				showhide_button.appendTo($('#content'));
				$('<a class="content-box-closer" href="#" />')
					.click(function(e) {
						e.preventDefault();
						showhide_button.trigger("click");
						return false;
					})
					.appendTo($('#content .content-box'));
					
				cd.pagetypes.ProjectPage.setup_prevnext();
			},
			setup_prevnext: function() {
				$('#prev_next a').click(function() {
					var ajax_url = this.href;
					$.getJSON(ajax_url, function(data) { 
						//data = jQuery.parseJSON(data);
						$('#prev_next').html(data.PhotosNav);
						cd.pagetypes.ProjectPage.setup_prevnext();
						var $img = $(data.Photo);
						$img.addClass('next').appendTo(cd.bgimage.holder);
						$img.load(function() {
							cd.bgimage.resize($img);
							cd.bgimage.show($img);
						});
					});

					return false;
				});
			}
		}
	} //pagetypes
};

(function($){
	
    // Creating custom :external selector
    $.expr[':'].external = function(obj){
        return !obj.href.match(/^mailto\:/)
                && (obj.hostname != location.hostname);
    };
    $('a:external').attr('target', '_blank').addClass("external");

	cd.init();
		
	$(window).focus(function() { 
		$('html').addClass("has-focus").removeClass("no-focus");
	}).blur(function() { 
		$('hmtl').addClass("no-focus").removeClass("has-focus");
	});
    
})(jQuery);
