var myTime;
$(document).ready(function(){

	var el1 = $('#nfc_cont');
	var nfc_l_height = el1.find('.boxleft1').attr('offsetHeight') - 20;
	var nfc_r_height = el1.find('.boxright1').attr('offsetHeight') - 32;
	if(nfc_l_height >= nfc_r_height)
		el1.css('background-color','#e6e6e6');
	else el1.css('background-color','#ffffff');	
	
	var el2 = $('#nwu_cont');
	var nfc_l_height = el2.find('.boxleft1').attr('offsetHeight') - 20;
	var nfc_r_height = el2.find('.boxright1').attr('offsetHeight') - 32;
	if(nfc_l_height >= nfc_r_height)
		el2.css('background-color','#e6e6e6');
	else el2.css('background-color','#ffffff');	
		
    $("a.fngal").fancybox({ 'zoomSpeedIn': 300, 'zoomSpeedOut': 300, 'overlayShow': true });
      
    $('ul#menu li.mlevel1').hover(
      function(){
        $(this).find('div.osubmenu').show();
        $(this).find('img:first').attr('src','images/menu_seperator_down.png');
        $(this).css({'background-image':'url("images/menu_arrow_down.png")','background-position':'3px 14px'});
      },
      function(){
        $(this).find('div.osubmenu').hide();
        $(this).find('img:first').attr('src','images/menu_seperator.gif');
        $(this).css({'background-image':'url("images/menu_arrow.png")','background-position':'6px 9px'});
      })
      
    //$('ul.submenu li:not(li.sep,li.seplast)').hover(function(){$(this).css({'background-color':'#0b0b70','margin':'0 3px'})},function(){$(this).css({'background-color':'','margin':'0'})})  
    $('ul.submenu li:not(li.sep,li.seplast)').hover(function(){$(this).css({'background-color':'#a00a0b','margin':'0 3px'})},function(){$(this).css({'background-color':'','margin':'0'})})      

    $('div#rmainnews img').click(function(){
    	//clearTimeout(myTime);
    	var el = $('div#lmainnews');
    	var targetNews = $(this).attr('id').split("_");
    	var currentNews = el.find('img').attr('id').split("_");
    	if(targetNews[1] == currentNews[1]) return false;
    	el.find('div#mainnewscontent, img').hide();    
    	el.find('img').attr({
    		id: 'mainimgid_'+targetNews[1],
    	    src: 'images/foto/artykuly/news/388/'+mainnews[targetNews[1]][2]
    	});
    	el.find('a').attr('href',mainnews[targetNews[1]][6]);
    	el.find('a').attr('title',mainnews[targetNews[1]][1]);    	
    	var content;
    	content = '<a class="newstitle block w280" href="'+mainnews[targetNews[1]][6]+'" title="'+mainnews[targetNews[1]][1]+'"><strong>'+mainnews[targetNews[1]][1]+'</strong></a>';
        content+= '<span class="newsdate block">';
        /*if(mainnews[targetNews[1]][3])
        	content += mainnews[targetNews[1]][3];
        if(mainnews[targetNews[1]][4])
        	content += ', autor: '+mainnews[targetNews[1]][4];
        if(mainnews[targetNews[1]][5])
        	content += ', źródło: '+mainnews[targetNews[1]][5];*/
        content += '<a href="'+mainnews[targetNews[1]][6]+'" title="'+mainnews[targetNews[1]][1]+'">'+mainnews[targetNews[1]][7]+'</a>';
        content += '</span>';
        el.find('div#mainnewscontent').html(content);

    	el.find('div#mainnewscontent, img').fadeIn(); //show    	   	

    }); 
    
    /* do przemyslen czasem nie dziala dobrze jak sie za szybko kilka ... callback, kolejka... */ 
    $('div#rmainnews img').mousedown(function(){ 
    	clearTimeout(myTime);
    	switchNews($(this).attr('id').split('_')[1]); 
    });
    
    $('input#newssave_email').click(function(){	      
    	$.ajax({
	        type: "POST",
	        url: mainurl+"/?module=newsletter&action=addemail",
	        data: "email="+$('input#newsletter_email').val()+
	        	  "&name="+$('input#newsletter_name').val()+
	        	  "&red="+$('input#newsletter_red').val()+
	        	  "&phone="+$('input#newsletter_phone').val()
	        ,
	        beforeSend: function(response){
	           $('form#fnewsletter input').attr("disabled", true);
	           $('td#newsletter_msg').html('Trwa przesyłanie danych');
	        }, 			
	        success: function(response){
	        	$('form#fnewsletter input').attr("disabled", false);
	            $('td#newsletter_msg').html(response);
	        }
	      });
    });

    $('#ac_kind').change(function(){
    	if($(this).selectedValues() == 'single'){    		
    		$.ajax({
    	        type: "POST",
    	        url: mainurl+"/?module=event&action=getnextmatchs",
    	        dataType: 'json',
    	        beforeSend: function(response){
    	           $('form#facreditation input,form#facreditation select').attr("disabled", true);
    	        }, 			
    	        success: function(response){
    	        	var matches = $('#ac_match');
    	        	matches.attr('disabled',false);
    	        	for(var i in response){    	        		
    	        		matches.addOption(response[i].id,response[i].w_nazwa+" - "+ response[i].data_meczu.replace(/:00$/,''));
    	        	}
    	        	matches.selectOptions('');
    	        	$('form#facreditation input,form#facreditation select').attr("disabled", false);	        		        
    	        }
    	      });    	    	
    	} else {
    		var matches = $('#ac_match');  
    		matches.removeOption(/./);
    		matches.attr('disabled',true);    		
    	}
    });

    
    $('input#acsave_acreditation').click(function(){	      
    	$.ajax({
	        type: "POST",
	        url: mainurl+"/?module=newsletter&action=sendAcreditation",
	        data: "ac_name="+$('input#ac_name').val()+
	        	  "&ac_pesel="+$('input#ac_pesel').val()+
	        	  "&ac_email="+$('input#ac_email').val()+
	        	  "&ac_red="+$('input#ac_red').val()+
	        	  "&ac_phone="+$('input#ac_phone').val()+
	        	  "&ac_kind="+$('select#ac_kind').selectedValues()+
	        	  "&ac_match="+$('select#ac_match').selectedValues()+
	        	  "&ac_type="+$('select#ac_type').selectedValues()
	        ,
	        beforeSend: function(response){
	           $('form#facreditation input,form#facreditation select').attr("disabled", true);
	           $('td#acreditation_msg').html('Trwa przesyłanie danych');
	        }, 			
	        success: function(response){
	        	$('form#facreditation input').attr("disabled", false);
	        	if(response == "Wiadomość została wysłana dziękujemy.")
	        		$('form#facreditation input:not(:button)').val('');
	            $('td#acreditation_msg').html(response);
	        }
	      });
    });
    
    if($('form#facreditation').length > 0){

    	$.ajax({
	        type: "POST",
	        dataType: 'json',
	        url: mainurl+"/?module=newsletter&action=getaccreditations",
	        beforeSend: function(response){
	        }, 			
	        success: function(response){
	        	if(response.length){
		        	var html = '<hr style="height:1px;border:0;background: #cfcfcf;" /><table style="font-size: 11px;" cellpadding="7" cellspacing="0" border="0">';
		        	html += '<tr><td colspan="4"><b>Przyznane akredytacje:</b></td></tr>'
	        		html += '<tr><th></th><th>Redakcja</th><th>Rodzaj</th><th>Typ</th><th>Mecz</th><th></tr>';
		        	var type = Array();
		        	type['press'] = 'Prasa';
		        	type['photo'] = 'Foto';
		        	type['tv'] = 'TV';	        	
		        	var kind = Array();
		        	kind['all'] = 'Stała';
		        	kind['single'] = 'Jednorazowa';	        		        	
		        	for(var i in response){	        		
		        		html += '<tr><td>'+response[i].ac_name+'</td><td>'+response[i].ac_red+'</td><td>'+kind[response[i].ac_kind]+'</td><td>'+type[response[i].ac_type]+'</td><td>'+response[i].nazwa+'<br />'+response[i].data_meczu.replace(/:00$/,'')+'</td><td></tr>';
		        	}
		        		
		        	html += '</table>';
		        	$(html).insertAfter($('form#facreditation'));
	        	}
	        }
	      });
    	
    }
    
    $('input#fcsave_card').click(function(){	      
    	$.ajax({
	        type: "POST",
	        url: mainurl+"/?module=newsletter&action=sendTSPCard",
	        data: "fc_fname="+$('input#fc_fname').val()+
	        	  "&fc_lname="+$('input#fc_lname').val()+	  
	        	  "&fc_pesel="+$('input#fc_pesel').val()+
	        	  "&fc_phone="+$('input#fc_phone').val()+
	        	  "&fc_number="+$('input#fc_number').val()+
	        	  "&fc_sector="+$('input#fc_sector').val()+
	        	  "&fc_place="+$('input#fc_place').val()
	        ,
	        beforeSend: function(response){
	           $('form#fcard input,form#fcard select').attr("disabled", true);
	           $('td#fcard_msg').html('Trwa przesyłanie danych');
	        }, 			
	        success: function(response){
	        	$('form#fcard input,form#fcard select').attr("disabled", false);
	        	if(response == "Dane zostały wysłane. Dziękujemy."){
	        		$('form#fcard input:not(:button)').val('');
	        		$('td#fcard_msg').html('');
	        		alert(response);	
	        	} else {
		            $('td#fcard_msg').html(response);
		        	        		
	        	}
	        }
	      });
    });    
    
    $('input#csend').click(function(){	      
    	var sendTo = '';
    	if($('input#only_spokesman').val() == undefined)
    		sendTo = $('select#csendto').val();
    	else sendTo = 'm.nikiel@ts.podbeskidzie.pl';    	
    	$.ajax({
	        type: "POST",
	        url: mainurl+"/?module=newsletter&action=sendContact",
	        data: "sendto="+sendTo+  
	        	  "&email="+$('input#cemail').val()+	  
	        	  "&title="+$('input#ctitle').val()+
	        	  "&content="+$('textarea#ccontent').val()
	        	  //only_spokesman
	        ,
	        beforeSend: function(response){
	           $('form#fcontact input,select,textarea').attr("disabled", true);
	           $('td#c_msg').html('Trwa przesyłanie danych');
	        }, 			
	        success: function(response){
	        	$('form#fcontact input,select,textarea').attr("disabled", false);
	        	if(response == "Wiadomość została wysłana dziękujemy."){
	        		$('form#fcontact input:not(:button),textarea').val('');
	        		$('form#fcontact select').selectOptions('');
	        	}
	            $('td#c_msg').html(response);
	        }
	      });
    });    
  
	var marquee = $('ul#marquee');
	marquee.find('img').fadeTo('fast',0.7);
	marquee.marquee(
			{ 
		        yScroll: "bottom"  
		       , scrollSpeed: 20 
		       , pauseSpeed: 0  
		});			
	marquee.marquee('resume');      
    
    switchNews(0);
    
  });

  function switchNews(id) {
	  //clearTimeout(myTime);
	  $('div#rmainnews img#imgid_'+id).click();

	  if(id==2)id = 0;
	  else id = id + 1;	  
  
	  myTime = window.setTimeout("switchNews("+id+")",5000);
  }
  
