(function(j){
	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 executeCufon(){
		Cufon.replace('h1,h2,h3,h4,h5, .content-title', {hover: 'true'});
		Cufon.replace('.button_pack', {hover: 'true', textShadow: '#AAA 0px 1px'});
	};
	
	function pagemenu(pageid){
		j(pageid + " ul").css({display: "none"});
		j(pageid).find('a').removeAttr('title');
		j(pageid + " ul").find('a:last').css({border: "0"});
		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(400,
					function(){
						j(this).css({overflow:"visible", height:"auto"});
					}
				);	
			}, function(){
				var biz = kl.find('.onhov');		
				j(this).removeClass('onhov');
				j(this).find('ul:first').stop().slideUp(400,
					function(){
						j(this).css({overflow:"hidden", display:"none"});
					}
				);			
			});
		})
		j(pageid +' .current-menu-item').find('a:first').addClass('pageactive');
	};
	
	function playTicker(){
		j('#hidenewsticker').themesguyTicker();
	};
	
	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;
			});
		
		});
	
	};
	
	function handleSearch(){
		var input = j('#input_wega_search');
		var hdiv = input.next('#shidden');
		
		input.focus(function(){
			if(j(this).val() == "Search")
			j(this).val("");
			
			if(hdiv.is(":hidden"))
				hdiv.slideDown('fast');
		});
		
		input.blur(function(){
			if(j(this).val() == "")
				j(this).val("Search");
		});
		
		input.click(function(){
			if(hdiv.is(":hidden"))
				hdiv.slideDown('fast');
		});
		
		hdiv.mouseleave(function(){
			if(input.not(":focus"))
				j(this).slideUp('fast');
		});
		
		j('#searchform').parent().mouseleave(function(){
			hdiv.slideUp('fast');
		});
	};
	
	function loadImg(){
		j('#preloader').tgPreload({'childClass' : 'img_roundedin_blog'});
	};
	
	function activateLighbox(){
		j("a[rel^='portFolio']").prettyPhoto({theme:'facebook'});
	};
	
	function inHoverImage(){
		var prTo = j("a[rel^='portFolio']");
			
		prTo.each(function(){
			j(this).hover(function(){
				var thisimg = j(this).find('img');
					
				thisimg.stop().animate({opacity : 0.6}, 300);

			}, function(){
				var thisimg = j(this).find('img');
	
				j(this).find('img').stop().animate({opacity : 1}, 300);

			});
		});
			
	};
	
/* 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();
			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(' *');
			});
		
	}
	
j(document).ready(function(){
	enterClass();
	pagemenu("#pagemenu");
	executeCufon();
	playTicker();
	handleTopScroll();
	handleSearch();
	ContactHandle();
});

j(window).load(function(){ loadImg(); activateLighbox(); inHoverImage(); });
})(jQuery);
