<!--
/**
 * The gallery System Front-end Core
 *
 *
 * @author     XuQian(AlloVince) <xuqian@easthv.com>
 * @copyright  2009 AlloVince
 * @link       http://www.easthv.com/
 */

(function(){

var 

window = this,

undefined ,

p = function (m,level){
	if(console === undefined || console.log === undefined)
	return;
	level == undefined ? 0 : level;
	var type = typeof(m);
	if(type == 'object') {
		for(var i in m) {
			var s = i + ' : ';
			if(typeof(m[i]) == 'object') {
				console.log("debug : [%s]",s);
				s = '';
				debug(m[i],level + 1);
			}
			else {
				s = i + ' : ' + m[i];

			}
			for(var j = 0; j < level; j++) {
				s = "\t" + s;
			}
			if(s) {
				console.log("debug : [%s]",s);
			}
		}

	}
	else {
		console.log("debug : [%s]",m);
	}
},

gallery = window.gallery = {

	root : '',

	debug : false,

	config : {
		action : '',
		dir : '',
		path : '/index/',
		useswf : false,
		photoloader : '/swf/photoloader.swf',
		column : 1,
		marginTop : 100,
		marginBottom : 25,
		sidebar : 210,
		testimonials : 300,
		radio : 1.488,
		percent : 0.49,
		resize : true,
		multi : true,
		images : [],
		images1 : [],
		images2 : []
	},

	galleryStatus : {
		loadColumn : 0,
		width : 0,
		height : 0
	},
		
	//do sth before load Jquery
	init : function(config){

		for(var i in config) {

			gallery.config[i] = config[i];
		
		}

		gallery.config.photoloader = gallery.config.dir + gallery.config.photoloader;

		gallery.initLoading();

		gallery.setPageWidth();

		var action = gallery.config.action;

		action = action + 'Action';

		if(gallery[action] === undefined)
			return;


		gallery[action]();

	},

	client : {
		msie : $.browser.msie,
		screenWidth : window.screen.width,
		screenHeight : window.screen.height,
		width : document.documentElement.clientWidth,
		height : document.documentElement.clientHeight
	},

	hideScroll : function(){
		
		$("html,body,.main-wrap,.sidebar,.extra,.main,.page,.body").css("height",'100%');

	},

	getWidth : function(){
	
		gallery.galleryStatus.height = gallery.client.height - gallery.config.marginTop - gallery.config.marginBottom;

		gallery.galleryStatus.width = parseInt(gallery.galleryStatus.height * gallery.config.radio);

		//browser size not match photo size
		if(gallery.galleryStatus.width - (gallery.client.width * 0.9 - gallery.config.sidebar) <= 50)
			return;
	
		gallery.galleryStatus.width = parseInt(gallery.client.width * 0.9 - gallery.config.sidebar);
		gallery.galleryStatus.height = parseInt(gallery.galleryStatus.width / gallery.config.radio);

	},

	setPageWidth : function() {
		
		gallery.getWidth();

		var pageWidth = gallery.galleryStatus.width + gallery.config.sidebar;

		$("#page").width(pageWidth);
		$("#header").width(pageWidth);
		$("#footer").width(pageWidth);
	},

	initLoading : function(){
		$("body").append('<div id="loading"><span>Now Loading</span></div>');
		$("#loading").hide();
		$("#loading").ajaxStart(gallery.showLoading).ajaxStop(gallery.hideLoading);
	},

	showLoading : function(){
		$("#loading").slideDown();	  
	},
	
	hideLoading : function(){
		$("#loading").slideUp();	  
	},
	
	//images array must be order by width DESC
	selectImage : function(height,images){
		var selected = {}

		for(var i in images) {
			selected = images[i];
			if(height < images[i].height) {
				continue;
			} else {
				break;
			}
		}

		if(images[i - 1])
			selected = images[i - 1];

		selected.src = gallery.config.path + selected.src;

		return selected;		  
				  
	},

	resizeByWidth : function(width,image) {
		var radio = image.width / image.height;
		var height = parseInt(width / radio);
		return {
			width : width,
			height : height
		}
	},

	resizeByHeight : function(height,image) {
		var radio = image.width / image.height;
		var width = parseInt(height * radio);
		return {
			width : width,
			height : height
		}
	},

	insert : function(selector,image,size) {
		
		$(selector).prepend('<img class="photo" src="' + image.src + '" alt="' + image.title + '" width="' + size.width + '" height="' + size.height + '" title="' + image.title + '" />');

		if(gallery.config.useswf === true)
			$(selector).empty().flash({
				swf: gallery.config.photoloader,
				width: size.width,
				height: size.height,
				flashvars: {
					url: image.src,
					width: size.width,
					height: size.height
				}
			});

	},

	oneColumn : function(){

		var selected = gallery.selectImage(gallery.galleryStatus.height,gallery.config.images);

		var size = gallery.resizeByWidth(gallery.galleryStatus.width,selected);

		gallery.insert(".gallery",selected,size);

	},

	twoColumn : function(){

		var selected = gallery.selectImage(gallery.galleryStatus.height,gallery.config.images1);

		var size = gallery.resizeByHeight(gallery.galleryStatus.height,selected);

		gallery.insert(".left",selected,size);

		var selected = gallery.selectImage(gallery.galleryStatus.height,gallery.config.images2);

		var size = gallery.resizeByHeight(gallery.galleryStatus.height,selected);

		gallery.insert(".right",selected,size);

	},


	doLoad : function(items,selector,column) {

		var length = items.length;
		var doneStatus = 0;
		var doneNow = 0;

		for (var i = 0; i < length; i++) {
			var imgLoad = $("<img></img>");
			$(imgLoad).attr("src", items[i].src);

			if(gallery.client.msie === true) {
				$(selector).css("visibility","inherit");
				if(column == 2) {
					$(selector).replaceImage(items,2);
				} else {
					$(selector).replaceImage(items,1);
				}
				gallery.hideLoading();
				return;
			}
			$(imgLoad).unbind("load");
			$(imgLoad).bind("load", function() {
				doneNow++;

				if(doneNow == length) {

					$(selector).css("visibility","inherit").hide().fadeIn('slow');

					if(column == 2) {
						$(selector).replaceImage(items,2);
					} else {
						$(selector).replaceImage(items,1);
					}

					gallery.galleryStatus.loadColumn++;
					if(gallery.galleryStatus.loadColumn == column) {
						gallery.hideLoading();
					}
				
				}
			});
		}
	},

	faqAction : function(){

		$("img").each(function(){
			if($(this).width() > $(this).parent().width()) {
				$(this).width("100%");
			}
		});
		
		var url = gallery.config.dir + '/index/faq/' + gallery.config.id;
		
		gallery.config.id ? $(".question a[href=" + url + "]").parent().addClass("current") : $(".question a:first").parent().addClass("current");

		//load all image link
		$(".showInside").each(function(){
			var imgLoad = $("<img></img>");
			$(imgLoad).attr("src", $(this).attr("href"));
		});

		$(".showInside").click(function(){
			var href = $(this).attr("href");
			
			$.modal('<img src="' + href + '" width="100%">', {
				//position : {top:"10%"},
				closeHTML:"<a href='#' class='modalCloseImg'>Close</a>",
				overlayClose:true
			});

			return false;

		});

		$(".over").mouseover(function(){
			var src = $(this).attr("src");
			src = src.replace(".gif",'over.gif');
			$(this).attr("src",src);
		}).mouseout(function(){
			var src = $(this).attr("src");
			src = src.replace("over.gif",'.gif');
			$(this).attr("src",src);			
		});

		$('.case_study').hide();
		$('.show_case_study').click(function (e) {
			var href = $(this).attr("href");
			var width = $(href).width() + 30;
			var height =  $(href).height();
			var clientHeight = $(window).height();
			height = height > clientHeight ? clientHeight - 50 : height - 50
			e.preventDefault();
			$(href).modal({
				closeHTML:"<a href='#' class='modalCloseImg'>Close</a>",
				containerCss:{
					background:"#333",
					border:"3px solid #444",
					padding:"10px",
					'font-size':"1.2em",
					color:"#FFF",
					height: height,
					padding:0,
					width:720
				},
				overlayClose:true
			});
		});
		  
	},

	creativeAction : function(){

	},

	aboutAction : function(){

		$(".form").validate();

	},

	testimonialsAction : function(){

		if(!gallery.config.images.length)
			return;


		var selected = gallery.selectImage(gallery.galleryStatus.height,gallery.config.images);

		var size = selected.width > selected.height ? gallery.resizeByWidth(gallery.galleryStatus.width - gallery.config.testimonials,selected) : gallery.resizeByHeight(gallery.galleryStatus.height,selected);

		gallery.insert(".testimonials",selected,size);

	},
	portfolio2Action : function() {

		gallery.indexAction();
	
	},
	portfolio3Action : function() {

		gallery.indexAction();
	
	},

	indexAction : function() {

		gallery.hideScroll();

		if(gallery.config.column == 2) {
			if(gallery.config.images1.length == 0 || gallery.config.images2.length == 0)
				return;
		} else {
			if(gallery.config.images.length == 0)
				return;
		}

		gallery.config.column == 2 ? gallery.twoColumn() : gallery.oneColumn();

	
	}
};

})();

-->

