var IframeFlag=false;
var ScrollableFlag=false;

var portfolio_item_w=220;
var portfolio_item_h=150;

var ScrollPosition = 0;

var CategoryArray = {};
CategoryArray.Web = "Web";
CategoryArray.EDM = "EDM/Banner";
CategoryArray.Print = "Printing";

function getCategoryName(obj_name){
	tmp = obj_name.replace("#","");
	return CategoryArray[tmp];
}

function gobacktolist(){
	$.scrollTo(ScrollPosition,500);
}

function LoadCaseStudy(obj){

	ScrollPosition = $(document).scrollTop();

	$("#CaseStudy").css("display","block");
	$("#CaseStudy .casestudy_title").html("");
	$("#CaseStudy .casestudy_short").html("");
	$("#CaseStudy .contents_div").html("");

	$.scrollTo("#CaseStudy",1000,{easing:'easeInOutQuart'});
	$("#CaseStudy .casestudy_title").html($("li.company",obj).html());
	$("#CaseStudy .contents_div").html($("li.contents",obj).html());
	$("#CaseStudy .casestudy_short").html($("li.project",obj).html());



	if(ScrollableFlag==true){
		removeItem('#CaseStudy .image_slide');
		tmp_obj=$("li.images_li",obj);
		$.each(tmp_obj,function(){
			tmp_html = $(this).html();
			tmp_html = tmp_html.replace(/\<!--/g,"");
			tmp_html = tmp_html.replace(/\-->/g,"");
			$(this).html(tmp_html);
		});

		$.each(tmp_obj,function(){
			addItem('#CaseStudy .image_slide',$(this));
		});
		SlideReload('#CaseStudy .image_slide');

	}else{
		var tmp_html=$("li.images",obj).html();
		tmp_html = tmp_html.replace(/\<!--/g,"");
		tmp_html = tmp_html.replace(/\-->/g,"");
		$("#CaseStudy .image_slide").html(tmp_html);
		$('#CaseStudy .image_slide').scrollable({size:1,loop:true,clickable:false,vertical:false,next:".image_slide_next",prev:".image_slide_prev"});
		ScrollableFlag=true;
	}
	CaseStudyImageLoad();
}

function CaseStudyImageLoad(){
	$("#CaseStudy .image_slide li img").load(function(){
		$(this).addClass("show");
	});
}

function AlertMsg(msg){
	$("#AlertMsg .AlertMsg_msg").html(msg);
	$("#AlertMsg").fadeIn(200);
}

function getSearchResult(val_ind,val_sol,main_obj,othercategories,orgin_obj){
	var tmp_obj = $("li.item",$(main_obj));
	var item_num=0;
	$.each(tmp_obj,function(){
		industry = $(this).attr("industry");
		solution = $(this).attr("solution");
		if(val_ind!="all" && val_sol!="all"){
			if(industry!=val_ind || solution!=val_sol){
				//$(this).addClass("item_unactive");
			}else{
				item_num++;
			}
		}else if(val_ind!="all" && val_sol=="all"){
			if(industry!=val_ind){
				//$(this).addClass("item_unactive");
			}else{
				item_num++;
			}
		}else if(val_ind=="all" && val_sol!="all"){
			if(solution!=val_sol){
				//$(this).addClass("item_unactive");
			}else{
				item_num++;
			}
		}else{
			item_num++;
		}
	});
	var tmpHTML="";
	if(item_num>0){
		if(othercategories==""){
			tmpHTML = "<br /><br />Or please have a look at other categories below:";
		}
		Category=getCategoryName(main_obj);
		tmpHTML += "<br /><a href='javascript:Search_portfolio(\"all\",\"all\",\""+orgin_obj+"\",false);$(\"#AlertMsg\").fadeOut(200,function(){$.scrollTo(\""+main_obj+"\",500);Search_portfolio(\""+val_ind+"\",\""+val_sol+"\",\""+main_obj+"\");$(\"select.industry\",$(\""+main_obj+"\")).getSetSSValue(\""+val_ind+"\");$(\"select.solution\",$(\""+main_obj+"\")).getSetSSValue(\""+val_sol+"\");});func_resetSS(\""+orgin_obj+"\");'>" + item_num + "project</a> found in "+Category;
	}
	return tmpHTML;
}

function Search_portfolio(val_ind,val_sol,main_obj,fadeinout){
	$("div.portfolio_loading",$(main_obj)).height($("ul.portfolio",$(main_obj)).height());

	if(fadeinout==false){
		$("li.item_unactive",$(main_obj)).removeClass("item_unactive");
		var tmp_obj = $("li.item",$(main_obj));
		var item_num=0;
		$.each(tmp_obj,function(){
			industry = $(this).attr("industry");
			solution = $(this).attr("solution");
			if(val_ind!="all" && val_sol!="all"){
				if(industry!=val_ind || solution!=val_sol){
					$(this).addClass("item_unactive");
				}else{
					item_num++;
				}
			}else if(val_ind!="all" && val_sol=="all"){
				if(industry!=val_ind){
					$(this).addClass("item_unactive");
				}else{
					item_num++;
				}
			}else if(val_ind=="all" && val_sol!="all"){
				if(solution!=val_sol){
					$(this).addClass("item_unactive");
				}else{
					item_num++;
				}
			}else{
				item_num++;
			}
		});
	}else{
		$("div.portfolio_loading",$(main_obj)).fadeIn(500,function(){
			$("li.item_unactive",$(main_obj)).removeClass("item_unactive");
			var tmp_obj = $("li.item",$(main_obj));
			var item_num=0;
			$.each(tmp_obj,function(){
				industry = $(this).attr("industry");
				solution = $(this).attr("solution");
				if(val_ind!="all" && val_sol!="all"){
					if(industry!=val_ind || solution!=val_sol){
						$(this).addClass("item_unactive");
					}else{
						item_num++;
					}
				}else if(val_ind!="all" && val_sol=="all"){
					if(industry!=val_ind){
						$(this).addClass("item_unactive");
					}else{
						item_num++;
					}
				}else if(val_ind=="all" && val_sol!="all"){
					if(solution!=val_sol){
						$(this).addClass("item_unactive");
					}else{
						item_num++;
					}
				}else{
					item_num++;
				}
			});
			Category="";
			Category=getCategoryName(main_obj);
			var othercategories="";
			if(item_num==0){
				othercategories += getSearchResult(val_ind,val_sol,"#Web",othercategories,main_obj);
				othercategories += getSearchResult(val_ind,val_sol,"#EDM",othercategories,main_obj);
				othercategories += getSearchResult(val_ind,val_sol,"#Print",othercategories,main_obj);
				AlertMsg("<p style='font-weight:normal;'>No projects in this section.<br /><b><a onclick='javascript:Search_portfolio(\"all\",\"all\",\""+main_obj+"\");$(\"#AlertMsg\").fadeOut(200);func_resetSS(\""+main_obj+"\")' style='cursor:pointer'>Click here</a></b> to see all projects."+othercategories+"</p>");
			}
		}).fadeOut(500);
	}
}


function func_resetSS(main_obj){
	$("select",$(main_obj)).getSetSSValue("all");
}

function viewCasestudy(item_idx){
	LoadCaseStudy($("#"+item_idx));
}

function LoadContactUs(){
//	alert($("div#ContactForm").html());
	tmp=$("div#ContactFormTMP").html();
	AlertMsg("<form name='contact_form' action='#' method=post><div id='ContactForm'>"+tmp+"</div></form>");

	$("div#ContactForm input[name='URL']").val(document.location.href);

	$("div#ContactForm .contactInput").focus(function(){
		tmp = $(this).offset()
		tmp_top = tmp.top * 1 - 14;
		tmp_left = tmp.left * 1 + $(this).width() + 10;
		$("div#ContactAlert").offset({ top: 0, left: 0 });
		$("div#ContactAlert").html($(this).attr("alt")).width(210).offset({ top: tmp_top, left: tmp_left });
		if($(this).val()==$(this).attr("alt")){
			$(this).val('');
		}
	});
	$("div#ContactForm .contactInput").blur(function(){
		if($(this).val()==''){
			$(this).val($(this).attr("alt"));
			$("div#ContactAlert").width(0);
		}
	});
	$("form").submit(function(){
		$("div#ContactAlert").width(0);
		chk_obj = $("div#ContactForm input.required");
		var flag=true;
		$.each(chk_obj,function(){
			$(this).removeClass("FormValidattion");
			if($(this).hasClass("email")){
				var emailRE = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;
				if(!emailRE.test($(this).val())){
					$(this).addClass("FormValidattion");
					flag=false;
				}
			}else{
				if($(this).val() == $(this).attr("alt")){
					$(this).addClass("FormValidattion");
					flag=false;
				}
			}
//var emailRE = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;
//if( f.email.value=='' || !emailRE.test(f.email.value) ){
		});

		if(flag==false){
			alert('Please enter vaild information.');
			return false;
		}

		$("div#ContactForm").prepend("<div class='ContactSending'></div>");
		$.ajax({
			url: '/portfolio_contact.php',
			data: "URL="+$("form input[name=URL]").val()+"&Name="+$("form input[name=Name]").val()+"&Company="+$("form input[name=Company]").val()+"&Phone="+$("form input[name=Phone]").val()+"&Email="+$("form input[name=Email]").val()+"&Message="+$("form input[textarea=Message]").val()+"",
			success: function(data) {
				if(data=="OK"){
					AlertMsg("Thank you for contacting us. <br />A member of our staff will be in touch as soon as possible. ");
				}else{
					AlertMsg("Sorry. The enquiry was not submitted successfully.<br />Please try again later. Thank you.");
				}
			}
		});
/*
	<input type="hidden" name="URL">
	<input type=text name="Name" value="Your Name?" alt="Your Name?" class="contactInput" />
	<input type=text name="Company" value="Your Company?" alt="Your Company?" class="contactInput" />
	<input type=text name="Phone" value="Your Telephone Number?" alt="Your Telephone Number?" class="contactInput" />
	<input type=text name="Email"  value="Your Email Address?" alt="Your Email Address?" class="contactInput" />
	<textarea name="Message" alt="How can we help you?" class="contactInput" >How can we help you?</textarea>
*/
//		return false;
//		return true;
		return false;
	});
}

var tmp_left;
$(document).ready(function (){

	if($.browser.msie && $.browser.version=="6.0"){
		$('<div></div>').html('This page may not be displayed correctly in this browser. You are strongly encouraged to update to a current release of <a href="http://mozilla.org/firefox">Firefox</a>')
			.activebar({
				'url': 'http://mozilla.org/firefox'
		});
	}

	$("div#ContactUsBtn").click(function(){
		LoadContactUs();
	});

	$("div#heroNav_box li").click(function(){
		var w=960;
		if($(this).hasClass("active")!=true){
			$("div#heroNav_box li.active").removeClass("active");
			$(this).addClass("active");

			var page = $(this).attr("page");
			page--;
			tmp_left=w*page*-1;
			//alert(w*page*-1);
			$("div#heroHTML_fade").fadeIn(200,function(){
				$("div#heroHTML_box ul").css("left",tmp_left);
					$("div#heroHTML_fade").fadeOut(500);
			});
		}
	});

	$("img#web_search").click(function(){
		val_1 = $("#web_industry").getSetSSValue();
		val_2 = $("#web_solution").getSetSSValue();
		Search_portfolio(val_1,val_2,"#Web");
	});
	$("img#edm_search").click(function(){
		val_1 = $("#edm_industry").getSetSSValue();
		val_2 = $("#edm_solution").getSetSSValue();
		Search_portfolio(val_1,val_2,"#EDM");
	});
	$("img#prt_search").click(function(){
		val_1 = $("#prt_industry").getSetSSValue();
		val_2 = $("#prt_solution").getSetSSValue();
		Search_portfolio(val_1,val_2,"#Print");
	});

	$("#web_industry,#web_solution,#edm_industry,#edm_solution,#prt_industry,#prt_solution").sSelect();
	$("#draggable").draggable();

	if(window.location.hash!=""){
		tmp_hash=window.location.hash;
		tmp_hash=tmp_hash.replace("#","");
		jQuery.each($("ul.portfolio li.item ul.details li.alias"),function(){
			if($(this).html().replace(" ","")==tmp_hash){
				LoadCaseStudy($(this).parent());
			}
		});
	}

	if(window.location.search!=""){
		tmp_hash=window.location.search;
		tmp_hash=tmp_hash.replace("?project=","");
		jQuery.each($("ul.portfolio li.item ul.details li.alias"),function(){
			if($(this).html().replace(" ","")==tmp_hash){
				LoadCaseStudy($(this).parent());
			}
		});
	}

	$("#AlertClose").click(function(){
		$("#AlertMsg").fadeOut(200);
	});	
	$("div.image_slide_enlarge").click(function(){
		var api = $("#CaseStudy .image_slide").scrollable();
		idx=api.getIndex();
		target_obj = $("li.images_li","#CaseStudy .image_slide")[idx];
		AlertMsg($(target_obj).html());
	});

	$("div.item_bottom a").click(function(){
		tmp_obj = $(this).parent().parent();
		LoadCaseStudy(tmp_obj);
	});
	$(".IframeClose").click(function(){
		$("#LaunchWebsite").fadeOut(500);
		$("iframe","#LaunchWebsite").attr("src","about:blank");
		IframeFlag=false;
	});



	$("a.LinkCase").click(function (){
		tmp_obj = $(this).parent().parent().parent();
		LoadCaseStudy(tmp_obj);
	});

	$("a.LinkLaunch").click(function (){
		tmp_obj = $(this).parent().parent().parent();
		$("#LaunchWebsite").fadeIn(2000);
		$("iframe","#LaunchWebsite").attr("src",$("li.url",tmp_obj).html());
		$("iframe","#LaunchWebsite").css("height",$(window).height()-96);
		IframeFlag=true;
	});

	$("li.item").mouseenter(function(e){

		$("div.item_over",$(this)).css("display","block").fadeTo(300,0.7);
		$("div.item_over_content",$(this)).css({top:-portfolio_item_h,width:portfolio_item_w,height:portfolio_item_h}).animate({top:0},200);

	});

	$("li.item").mouseleave(function(e){

		$("div.item_over",this).fadeOut(300);
		$("div.item_over_content",this).animate({top:-portfolio_item_h},200);

	});


	$("#navigation li.home a").click(function(){
		if(IframeFlag==false){
			$.scrollTo("#Top",1000,{easing:'easeInOutQuart'});
		}else{
			AlertMsg("Please close launched website first.");
		}
	});
	$("#navigation li.web a").click(function(){
		if(IframeFlag==false){
			$.scrollTo("#Web",1000,{easing:'easeInOutQuart'});
		}else{
			AlertMsg("Please close launched website first.");
		}
	});
	$("#navigation li.edm a").click(function(){
		if(IframeFlag==false){
			$.scrollTo("#EDM",1000,{easing:'easeInOutQuart'});
		}else{
			AlertMsg("Please close launched website first.");
		}
	});
	$("#navigation li.print a").click(function(){
		if(IframeFlag==false){
			$.scrollTo("#Print",1000,{easing:'easeInOutQuart'});
		}else{
			AlertMsg("Please close launched website first.");
		}
	});
});

$(function() {
	var d=300;
	var a_height = 100;
	$('#navigation a').each(function(){
		$(this).stop().animate({
			'marginTop':'-'+(a_height-20)+'px'
		},d+=150);
	});

	$('#navigation > li').hover(
	function () {
		$('a',$(this)).stop().animate({
			'marginTop':'-22px'
		},500,"easeOutElastic");
	},
	function () {
		$('a',$(this)).stop().animate({
			'marginTop':'-'+(a_height-20)+'px'
		},200);
	}
);
});



function addItem(target_obj,item_obj) { 
	// get handle to scrollable API 
	var api = $(target_obj).scrollable(); 

	// append new item using jQuery's append() method 
	api.getItemWrap().append($(item_obj).clone()); 

} 

function SlideReload(target_obj) { 
	var api = $(target_obj).scrollable(); 
	api.reload().begin();

}

function removeItem(target_obj) { 
	// get handle to scrollable api 
	var api = $(target_obj).scrollable(); 
	api.getItems().remove(); 
}
