(function(j){


		function getCookie(c_name)
		{
		var i,x,y,ARRcookies=document.cookie.split(";");
		for (i=0;i<ARRcookies.length;i++)
		{
		  x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
		  y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
		  x=x.replace(/^\s+|\s+$/g,"");
		  if (x==c_name)
		    {
		    return unescape(y);
		    }
		  }
		}

		function alert_it(type)
		{
				j('#growl'+type).fadeIn('slow',function(){
					j(this).delay(2000).fadeOut('slow');
				});
		}

		function form_valid()
		{
			var ok = 1;

			if((!checkEmail(j('.email').attr("value"))) || (j('.email').attr("value") == 'My Email is'))
			{
				ok = 0;
				j('.email').addClass("field_incomplete");
			}

			if(j('.name').attr("value") == '' || j('.name').attr("value") == 'My Name is')
			{
				ok = 0;
				j('.name').addClass("field_incomplete");
			}

			if(j('textarea').attr("value") == '' || j('textarea').attr("value") == 'My Phone number is')
			{
				ok = 0;
				j('textarea').addClass("field_incomplete");
			}

			return ok;
		}

		function checkEmail(str) {
			
			var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;  
  			return emailPattern.test(str);  
		}

		function get_v(str){
			switch(str)
			{
				case "name":
				  return "My Name is";
				case "email":
				  return "My Email is";
				case "phone":
				  return "My Phone number is";
				case "message":
				  return "My message is";
			}
		}

		function initMihnea()
		{

			j('.band div').fadeIn(3000);

			j('#cont_form input, textarea').focus(function(){
				if( (j(this).attr("value") == 'My Name is')||
					(j(this).attr("value") == 'My Email is')||
					(j(this).attr("value") == 'My Phone number is')||
					(j(this).attr("value") == 'My message is'))
						j(this).attr("value",'');
			});

			j('#cont_form input:not(.phone), textarea').blur(function(){
				
				if(j(this).attr("value") == '')
				{
					j(this).addClass('field_incomplete');
					j(this).val(get_v(j(this).attr("name")));
				}
				else
				{
					j(this).removeClass('field_incomplete');
					if((j(this).attr("name") == 'email')&&(!checkEmail(j(this).attr("value"))))
						j(this).addClass('field_incomplete');
				}
			});

			j('.phone').blur(function(){
				if(j(this).attr("value") == '')
				{
					j(this).val(get_v(j(this).attr("name")));
				}
			});
			
			j('#form_submit').live('click', function(){
				if(!form_valid())
				{
					alert_it(1);
				}
				else
				{
				
				
			  	  j.post('send_mail.php',  
			        { name: jQuery('.name').attr('value'), email: jQuery('.email').attr('value'),
			        phone: jQuery('.phone').attr('value'), text: jQuery('textarea').attr('value')
			        });                                            
			            alert_it(2);
			            j('#cont_form input, textarea').attr('value','');
			            


			       
				}

			});


			j('.services_large ul').find(":first").addClass("active");
			j('.services ul').find(":first").addClass("active");

			j('.services li').click(function(){
				var pid = j(this).attr("tid");
				j('.services_large .active').fadeOut(function(){
					j(this).removeClass("active");
					j('.services_large li[tid='+pid+']').fadeIn(function(){
						j(this).addClass("active");
					});
				});
			});

			count1 = 1; //closed
			var screenHeight = screen.height;
			var percent = (70/screenHeight)*100;
			percent = 100-percent-1.3;

			if ( j.browser.msie ) {
			  j("#head_info").slideUp(300)
			}
			j("#head_info_open").click(function(){
				if(count1 == 0)
				{
					j(this).animate({top: 0}, 300);
					j("#head_info").slideUp(300);
					j("#head_info_open").html("Client area");
					j("#top").animate({"padding-top": "10px",  "padding-bottom": "10px"}, 300);
					count1 = 1;
				}
				else
				{
					j(this).animate({top: 68}, 300);
					j("#head_info").slideDown(300);
					j("#head_info_open").html("Close");
					j("#top").animate({"padding-top": "40px", "padding-bottom": "0"}, 300);
					count1 = 0;
				}
			});

			j(".socialize img").hover(function(){
				j(this).animate({"margin-top":"-5px"}, 100);
			},function(){
				j(this).animate({"margin-top":"0"}, 100);
			});

			count = 0;
			var screenWidth = screen.width;
			var percent2 = (230/screenWidth)*100;
			percent2 = 100-percent2;
			j("#contact, #contactf, #contact_ico").click(function(){
				if(count == 0)
				{
					j("head_info").animate({ "margin-left":'-230px' });
					j("body").animate({ "margin-left":'-230px' });
					j("#pattern").animate({"width": percent+'%'});
					j("#contact_space").animate({ "right":"0" });
					j("#contact").addClass("ac");

					count = 1;
				}
				else
				{
					j("head_info").animate({ "margin-left":'0' });
					j("body").animate({	"margin-left":'0' });
					j("#pattern").animate({"width": 100+'%'});
					j("#contact_space").animate({ "right":"-230px" });
					j("#contact").removeClass("ac");

					count = 0;
				}
			});

			j("#contact_space_close").click(function(){
				j("body").animate({	"margin-left":'0' });
				j("#contact_space").animate({ "right":"-230px" });
				j("#contact").removeClass("ac");
				count = 0;
			});

			j(".anchor-to").click(function(){
				changeBg(j(this).attr("bg")); 
				j("#muta").animate({
					width: j(".press").outerWidth()-3,
					left: j(".press").position().left
				});

			});

			if(getCookie("page_show"))
			{
				j(".anchor-to[bg=bg_anchor0]").parent().removeClass("press");
				changeBg("bg_anchor" + getCookie("page_show"));
				j(".anchor-to[bg=bg_anchor"+ getCookie("page_show") +"]").parent().addClass("press");
				j('#logo a').click(function(){
					j(".anchor-to[bg=bg_anchor0]").parent().addClass("press");
				});
			}else{
				changeBg("bg_anchor0");
				j(".anchor-to[bg=bg_anchor0]").parent().addClass("press");
				j('#logo a').click(function(){
					j(".anchor-to[bg=bg_anchor0]").parent().addClass("press");
				});
			}

			

			/********** aici scriem meniul ********/
			var currentItem = j(".press");

			j("#muta").css({
				width: currentItem.outerWidth()-3,
				height: currentItem.outerHeight(),
				left: currentItem.position().left,
				top: currentItem.position().top
			});
		}
	
		function changeBg()
		{
			var newBg = "#"+arguments[0];
			if(!j(newBg).is(':visible'))
			{
				j(newBg).fadeIn("slow",function(){
					j(this).removeClass("hid");
				});
				j(".background:not(.hid)").fadeOut("5", function(){
					j(this).addClass("hid")
				});
			}
		}


		/* === CUFON === */
		function doCufon()
		{
			Cufon.replace('h1,h2,h3,h4,h5, .content-title', {hover: 'true'});

			if ( (j.browser.msie) && (parseInt(j.browser.version) == 8) ) {
				/*do nothing with cufon*/
			} else {
			  Cufon.replace('.tooltip_right, .two-columns strong', {hover: 'true'});
			}

		}
		
		/*The Animated Div*/
		function doAction()
		{
			/*Call the Scollpane plugin*/
			j('.texts').jScrollPane({showArrows : true, scrollbarWidth: 5, arrowSize: 1});
			
			
			j('.anchor-to').each(function(){
				j(this).bind('click', function(){
					
					if(j(this).attr('ttid'))
					{
						var ppid = j(this).attr("ttid");
						j('.services_large .active').fadeOut(function(){
							j('.active').removeClass("active");
							j('.services_large li[tid='+ppid+']').fadeIn(function(){
								j(this).addClass('active');
								j('.services li[tid='+ppid+']');
							});
						});
					}

					var anchor = j(this).attr('href');
					var id = j(this).attr('rel');
					var boxActive = j('.show');
					var antitle = j(this).next('span').text();
					var totitle = antitle + '  ' + j("meta[name=site_name]").attr('content');
					
					if( id != "blog"){
						/*if var id = 0, its mean the landing page(div) is called, than make litle different with site title*/
						if(id == "0") totitle = j("meta[name=site_name]").attr('content') + ' | ' + j("meta[name=site_desc]").attr('content');

								if( (j(anchor).css('top')) != '0px'){
									j('.press:not(#contact)').removeClass('press');
									var ftxt = j(this).attr('bg');
									j("li a[bg="+ftxt+"]").parent('li').addClass('press');
									
									
									j('.left_wrap').animate({width : '35px', bottom: '0px', left : '437px'}, 150);
									j('.right_wrap').animate({width : '35px', bottom: '0px', right : '437px'}, 150);
									
									boxActive.animate({top : '-700px'}, 150, function(){
										j(this).removeClass('show');
										
										/*animate the shadow*/
										j('.left_wrap').delay(500).animate({width : '470px', bottom: '0px', left : '0px'}, 200);
										j('.right_wrap').delay(500).animate({width : '470px', bottom: '0px', right : '0px'}, 200);
										
										/*Change all Scroll into TOP*/
										var elsa = j('.texts'); 
										for (var i=0; i<elsa.length; i++) {
											elsa[i].scrollTo(0);
										}
										
										/*if the anchor == portfolio div then call the different function.*/
										if( id == j("meta[name=portfolio_id]").attr('content') ){
										
											j('.portfolio_thumb').find('img').css('display', 'none');
											j('.keker').removeClass('keker');
											
											j('#thisport').delay(500).animate({top: '0px'}, 200, function(){
												j(this).addClass('show');
												j('#portfolio_lists').tgPreload({childClass:'inside_port'});
												
												/*Write the new title, based on page title*/
												if(j.browser.msie){
													document.title = totitle;
												} else {
													j('title').text(totitle);
												}

												
											});
											
										} else {
										
											j(anchor).delay(500).animate({top: '0px'}, 200, function(){
												j(this).addClass('show');
												
												/*Write the new title, based on page title*/
												if(j.browser.msie){
													document.title = totitle;
												} else {
													j('title').text(totitle);
												}
												
											});
											
										}

										

										/*Call the ajax to create the Cookie*/
										cookiesHandler(id);
									});
								}

						return false;
					} else {
						return true;
					}
				});
				
			});
			
			/*Throw the cookies with ajax. to remember the last visited div*/
			function cookiesHandler(id){
						var theUrl = j("meta[name=info_url]").attr('content');
						var theId = id;

									j.ajax({
									   type: "POST",
									   url: theUrl,
									   data: "change_page=true&pg_id=" + theId,
									   success: function(){
											return;
									   }
									 });
					return;
			}			
		}
		
/* ==== AJAX handle the portfolio pagination ==== */		
		function doAjaxforP(){
			
			j('.page_ajax').each(function(){
				
				j(this).bind('click', function(){
					
					if(j(this).hasClass('current_page')){
						/*do nothing*/
					} else {
						
						var theUrl = j("meta[name=info_url]").attr('content');
						var page = j(this).attr('rel');
						j('#portfolio_lists').find('img').remove();
						j('#portfolio_lists').find('div').removeClass('keker').addClass('ploader');
						document.cookie ='pages='+page+'path=/';
						j.ajax({
							type: "POST",
							url: theUrl,
							data: "action=true&pages=" + page ,
							success: function(msg){
	
									//j('a[rel^="prettyPhoto"]').prettyPhoto({theme:'facebook'});
									j('#portfolio_lists').find('div').fadeOut('fast'); 
										/*create cookie to remember the last page*/
									j('#portfolio_lists').html(msg);
									j('#portfolio_lists').tgPreload({childClass:'inside_port'});
									inHoverImage();
							}
						});
						
						j('.current_page').removeClass('current_page');
						j(this).addClass('current_page');
					}
					
					return false;
				});
			
			})
			
		}

/* ==== social media link, hover effect ==== */
		function mediaHover(){
			var social = j('#social').find('a');
			social.each( function(){
				j(this).hover(function(){
					j(this).stop().animate({top : '-4px'}, 200);
				}, function(){
					j(this).stop().animate({top : '0px'}, 200);
				});
			});
		}

/*hover effect in portfolio items*/		
		function inHoverImage(){
			var prTo = j('#portfolio_lists').find('a');
			
			prTo.each(function(){
				j(this).hover(function(){
					var thisimg = j(this).find('img');
					
					if( thisimg.css('top') == '0px' ){/*only do the effect when item has top = 0px */					
						thisimg.stop().animate({opacity : 0.6}, 300);
						j(this).parent().find('.zoom_icon').stop().animate({opacity : 0.4}, 300);
					}
					if(j(this).parent().hasClass('ploader')){
						j(this).parent().removeClass('ploader').addClass('keker');
					}
				}, function(){
					var thisimg = j(this).find('img');
					if( thisimg.css('top') == '0px' ){/*only do the effect when item has top = 0px */	
						j(this).find('img').stop().animate({opacity : 1}, 300);
						j(this).parent().find('.zoom_icon').stop().animate({opacity : 0}, 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 My 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 pageLoad(){
		j("a[rel^='prettyPhoto']").prettyPhoto({theme:'facebook'});
	}
	
	function attach(){
		j(window).load(function(){
			pageLoad();
		});
	}
	
	/*Call the functions when DOM ready*/
	j(document).ready(function(){
		doCufon();
		doAction();
		mediaHover();
		ContactHandle();
		doAjaxforP();
		pageLoad();
		initMihnea();

		j(document).ajaxStart(function(){ j('.ppt,.pp_overlay,.pp_pic_holder').remove(); }).ajaxStop(function(){ pageLoad() });
	});	

	j(window).load(function() {
		if( (j('#thisport').css('top')) == '0px' ){
			j('#portfolio_lists').tgPreload({childClass:'inside_port'});/*do the preload image when the specific div shown*/
		}
		inHoverImage();
	});

	
})(jQuery.noConflict());
