(function(a){
	function enterClass(){
		var menu = a('#menu').find('div.the_menu');
		
		if(menu.length != 0){
			var nextUL = menu.find('ul:eq(0)');
			
			nextUL.attr('id', 'pagemenu');
		}
	}
	
	function cuf(){
		Cufon.replace('h1,h2,h3,h4,h5,h6, .section_more, .rss_section, .unstrong, .portfolio_play, .content-title', {hover: true});
	}
	
	function pagemenu(pageid){
		a(pageid + " ul").css({display: "none"});
		a(pageid).find('a').removeAttr('title');
		a(pageid + " ul").find('a:last').css({border: "0"});
		var kl = a(pageid + " li").filter(":has(>ul)");
		
		kl.each(function(){
			a(this).hover(function(){
				a(this).addClass('onhov');
				a(this).find('ul:first').stop().css({overflow:"hidden", height:"auto",visibility: "visible",display: "none"}).slideDown(400,
					function(){
						a(this).css({overflow:"visible", height:"auto"});
					}
				);	
			}, function(){
				var biz = kl.find('.onhov');		
				a(this).removeClass('onhov');
				a(this).find('ul:first').stop().slideUp(400,
					function(){
						a(this).css({overflow:"hidden", display:"none"});
					}
				);			
			});
		})
		a(pageid +' .current_page_item').find('a:first').addClass('pageactive');
	};
	
	function portfolioH(){
		var pc = a('.portfolio_image_out');
		
		pc.each(function(){
			var tl = a(this).find('.portfolio_play');
			var pm = a(this).find('.portfolio_mores');
			var img = a(this).find('img');
			a(this).hover(function(){
				
				img.stop().animate({opacity : 0.5}, 200);
				tl.stop().animate({top : '73px'}, 200);
				pm.stop().animate({top : '73px'}, 200);
				
			}, function(){
				
				img.stop().animate({opacity : 1.0}, 200);
				tl.stop().animate({top : '-30px'}, 200);
				pm.stop().animate({top : '176px'}, 200);
				
			});
		});
	};
	
	function portfolioSinglejs(){
		if( a('#playground').length > 0 ){
			var theBigP = a('#small_thumb_lists > li:first').find('a');
			var genlink = a('#small_thumb_lists').find('a');
			var obj = theBigP.attr('href');
			
			theBigP.addClass('playing');
			doNiceThing(obj);
			
			genlink.each(function(){
				a(this).click(function(){
					var nobj = a(this).attr('href');
					a('.playing').removeClass('playing');
					a(this).addClass('playing');
					
					doNiceThing(nobj);
					return false;
				});
			});
		}
	};
	
	function doNiceThing(obj){
		var image_src = a("meta[name=theme_url]").attr('content') + 'timthumb.php?src={image}&h=250&w=480&zc=1&q=100';
		var flash_mark = '<object style="z-index:0;" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="{width}" height="{height}"><param name="wmode" value="transparent" /><param name="allowfullscreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="movie" value="{path}" /><embed src="{path}" type="application/x-shockwave-flash" allowfullscreen="true" allowscriptaccess="always" width="{width}" height="{height}" wmode="transparent"></embed></object>';
		var quicktime_mark = '<object classid="clsid:02bf25d5-8c17-4b23-bc80-d3488abddc6b" codebase="http://www.apple.com/qtactivex/qtplugin.cab#version=6,0,2,0" height="{height}" width="{width}"><param name="src" value="{path}"><param name="autoplay" value="true"><param name="scale" value="tofit"><param name="type" value="video/quicktime"><embed src="{path}" scale="tofit" height="{height}" width="{width}" autoplay="true" type="video/quicktime" pluginspage="http://www.apple.com/quicktime/download/"></embed></object>';
		a('#playground').addClass('ploader').html("");
		
			switch( joo_getFileType(obj) ){
					case 'image':
						var img = new Image();
						var tosrc = image_src.replace(/{image}/g,obj);
						
						a(img).css({'display' : 'block', 'opacity' : '0'}).load(function(){
							a(this).appendTo('#playground');
							a(this).animate({ opacity : 1}, 300, function(){
								a('#playground').removeClass('ploader');
							});
						}).attr('src', tosrc);
					break;
					case 'youtube':
						var w = 480;
						var h = 250;
						var movie = 'http://www.youtube.com/v/'+joorang_grab_param('v', obj);
						var insert = flash_mark.replace(/{width}/g,w).replace(/{height}/g,h).replace(/{path}/g,movie);
						
						a('#playground').removeClass('ploader');
						a(insert).appendTo('#playground');
					break;
					case 'vimeo':
						var w = 480;
						var h = 250;
						var movie = "http://vimeo.com/moogaloop.swf?clip_id="+ obj.replace('http://vimeo.com/','');
					
						var insert = flash_mark.replace(/{width}/g,w).replace(/{height}/g,h).replace(/{path}/g,movie);
						a('#playground').removeClass('ploader');
						a(insert).appendTo('#playground');
					break;
					case 'quicktime':
						var w = 480;
						var h = 250;
						var movie = obj;
					
						var insert = quicktime_mark.replace(/{width}/g,w).replace(/{height}/g,h).replace(/{path}/g,movie);
						a('#playground').removeClass('ploader');
						a(insert).appendTo('#playground');
					break;
					case 'flash':
						var w = 480;
						var h = 250;
						var insert = flash_mark.replace(/{width}/g,w).replace(/{height}/g,h).replace(/{path}/g,obj);
						a('#playground').removeClass('ploader');
						a(insert).appendTo('#playground');
					break;
			}
	}
	
	function loadImg(){
		a('#preloader').tgPreload({'childClass' : 'portfolio_image_in'});
	};
	
	function doPretty(){
		a("a[rel^='prettyPhoto']").prettyPhoto({theme:'facebook'});
		a("#tolightbox, .portfolio_play").click(function(){ return false; });
	};
	
/* contact form handle */	
	function ContactHandle()
	{
		a('#contact_submit').click( function() {
		
			var name = a('#hname').val();
			var mail = a('#hmail').val();
			var subs = a('#hsubj').val();
			var mess = a('#hmess').val().replace(/\r?\n|\r/g, "<br>");
			var sendto = a('#sendto').val();
			
			a('.contactload').fadeIn('fast');
			
			if (name != "" && mail != "" && subs != "" && mess != "")
				{
					var uril = a("#urlto").val();

					a.ajax(
						{
							url: uril,
							type: 'GET',
							data: "hname=" + name + "&hmail=" + mail + "&hsubj=" + subs + "&hmess=" + mess + "&sendto=" + sendto,
							success: function(result) 
							{
								a('.contactload').fadeOut('fast');
								if(result == "email_error") {
									a('#hmail').next('.req').html(' ! <small>please enter your valid email address</small>');
								} else {
									a('#hname, #hmail, #hsubj, #hmess').val("");
									a('<p id="contact_success">' + result + '<span class="jq_close"></span></p>').insertBefore('#adm-contact');
									a('.jq_close').click(function(){
										a(this).parent().fadeOut(300, function(){ a(this).remove(); });
									});
								}
							}
						}
					);
					return false;
					
				} 
			else 
				{
					a('.contactload').fadeOut('fast');
					if(name == "") a('#hname').next('.req').text(' !');
					if(mail == "") a('#hmail').next('.req').text(' !');
					if(subs == "") a('#hsubj').next('.req').text(' !');
					if(mess == "") a('#hmess').next('.req').text(' !');
					return false;
				}
		});
		
		a('#hname, #hmail, #hsubj, #hmess').focus(function(){
			a(this).next('.req').text(' *');
		});
		
	};
	
	function scrollit(){
		a.fn.hoverscroll.params = a.extend(a.fn.hoverscroll.params, {
			vertical:true,
			width: 105,
			height: 286,
			arrows: false
		});
		
		a('#small_thumb_lists').hoverscroll();	
	}
	
a(document).ready(function(){
	cuf(); 
	pagemenu("#pagemenu");
	doPretty();
	portfolioSinglejs();
	ContactHandle();
});

a(window).load(function(){ 
	loadImg(); 
	portfolioH();
	scrollit();
});
})(jQuery);

function joorang_grab_param(name,url){
	name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	var regexS = "[\\?&]"+name+"=([^&#]*)";
	var regex = new RegExp( regexS );
	var results = regex.exec( url );
	if( results == null )
		return "";
	 else
	return results[1];
};

function joo_getFileType(itemSrc){
	if(itemSrc.match(/youtube\.com\/watch/i)){
		return 'youtube';
	}else if(itemSrc.match(/vimeo\.com/i)){
		return 'vimeo';
	}else if(itemSrc.indexOf('.mov')!=-1){
		return 'quicktime';
	}else if(itemSrc.indexOf('.mp4')!=-1){
		return 'quicktime';
	}else if(itemSrc.indexOf('.3gp')!=-1){
		return'quicktime';
	}else if(itemSrc.indexOf('.swf')!=-1){
		return 'flash';
	}else{
		return 'image';
	};
};
