(function(j){
	function executeCufon(){
		Cufon.replace('h1,h2,h3,h4,h5,.bigbutton, .box_out_link, #pagemenu a', {hover: 'true'});
		Cufon.replace('#the_title h1 span, .content-title', {hover: 'true', textShadow: '0px 1px #FFF'});
	};
	
	function enterClass(){
		var menu = j('#menu').find('div.the_menu');
		
		if(menu.length != 0){
			var nextUL = menu.find('ul:eq(0)');
			
			nextUL.attr('id', 'pagemenu');
		}
	}
	
	function pagemenu(pageid){
		j(pageid + " ul").css({display: "none"});
		j(pageid).find('a').removeAttr('title');
		var kl = j(pageid + " li").filter(":has(>ul)");
		
		kl.each(function(){
			j(this).hover(function(){
				j(this).addClass('onhov');
				j(this).find('ul:first').stop().css({overflow:"hidden", height:"auto",visibility: "visible",display: "none"}).slideDown(300,
					function(){
						j(this).css({overflow:"visible", height:"auto"});
					}
				);	
			}, function(){
				var biz = j(this);		
				j(this).find('ul:first').stop().css({overflow:"hidden", display:"none"});
				biz.removeClass('onhov');			
			});
		})
		j(pageid +' .current_page_item').find('a:first').addClass('pageactive');
		j(pageid +' .current-menu-item').find('a:first').addClass('pageactive');
		
		j(pageid + " li").each(function(){
		var nuri = j(this).find('a:first').attr('href');
			j(this).click(function(){
				self.location.href = nuri;
			});
		});
	}
	
	function doLoader(){
		j('#need_loader_js').tgPreload({ childClass : "img_preloader"});
	};
	
	function afterloader(){
		var imglength = j('.portfolio_image_preview').length;
		
		if( imglength > 0 ){

				j('.portfolio_image_preview').each(function(){
					j(this).hover(function(){
						var thisimg = j(this).find('img');
							
						thisimg.stop().animate({opacity : 0.5}, 500);
					}, function() {
						var thisimg = j(this).find('img');
	
						j(this).find('img').stop().animate({opacity : 1}, 500);
					});
				})
		}
	};
	
	function doPretty(){
		var gal = j('.gallery').find('a');
		gal.each(function(){
			if(this.href.match(/\.(jpe?g|png|bmp|gif|tiff?)$/i)){
				j(this).attr('rel', 'prettyPhoto[gallery]');
			}
		});
	
		j("a[rel^='prettyPhoto']").prettyPhoto({theme:'facebook'});
	};
	
/* contact form handle */	
	function ContactHandle()
	{
		j('#contact_submit').click( function() {
		
			var name = j('#hname').val();
			var mail = j('#hmail').val();
			var subs = j('#hsubj').val();
			var mess = j('#hmess').val().replace(/\r?\n|\r/g, "<br>");
			var sendto = j('#sendto').val();
			
			j('.contactload').fadeIn('fast');
			
			if (name != "" && mail != "" && subs != "" && mess != "")
				{
					var uril = j("#urlto").val();

					j.ajax(
						{
							url: uril,
							type: 'GET',
							data: "hname=" + name + "&hmail=" + mail + "&hsubj=" + subs + "&hmess=" + mess + "&sendto=" + sendto,
							success: function(result) 
							{
								j('.contactload').fadeOut('fast');
								if(result == "email_error") {
									j('#hmail').next('.req').html(' ! <small>please enter your valid email address</small>');
								} else {
									j('#hname, #hmail, #hsubj, #hmess').val("");
									j('<p id="contact_success">' + result + '<span class="jq_close"></span></p>').insertBefore('#adm-contact');
									j('.jq_close').click(function(){
										j(this).parent().fadeOut(300, function(){ j(this).remove(); });
									});
								}
							}
						}
					);
					return false;
					
				} 
			else 
				{
					j('.contactload').fadeOut('fast');
					if(name == "") j('#hname').next('.req').text(' !');
					if(mail == "") j('#hmail').next('.req').text(' !');
					if(subs == "") j('#hsubj').next('.req').text(' !');
					if(mess == "") j('#hmess').next('.req').text(' !');
					return false;
				}
		});
		
		j('#hname, #hmail, #hsubj, #hmess').focus(function(){
			j(this).next('.req').text(' *');
		});
		
	};
	
	function handleTopScroll()
	{
		var anchor = j('.jhandletop').find('a');
		
		anchor.each( function()
		{
		
			j(this).click(function()
			{
				j.browser.opera  = /opera/.test(navigator.userAgent.toLowerCase());  
				if (j.browser.opera){
					j('html').animate({scrollTop : 0}, 'slow' );
				} else {
					j('html,body').animate({scrollTop : 0}, 'slow' );
				}	
				return false;
			});
		
		});
	
	};
	
j(document).ready(function(){
	executeCufon();
	enterClass();
	pagemenu('#pagemenu');
	doLoader();
	doPretty();
	ContactHandle();
	handleTopScroll();
});

j(window).load(function(){
	afterloader();
});
})(jQuery);
