
// ======================
function switchs()
{
	/**/
	$active = $('#top_news_active');
	$totle = $('.more_top_news li').size();
	if( $active.text() == $totle )  $active.text(0);
	
	//switchs_type();
	//clearTimeout(switchTime);
//	}else{
	
	
	//$('.news_line ul li').slideUp();
	//$('.news_line ul li').eq($active).show();
	//$('.points b').removeClass('act');
	//$('.points b').eq($active).addClass('act');
	
    me = $('#top_news_moer_' + $active.text())
	
	$('.top_news #title a').text(me.attr('title'));
	$('.top_news #title a').attr('href', $(me).attr('href') );

	$('.top_news').css('background-image', 'url(' + $('img.load_img',me).attr('src') + ') ' );
	$('.top_news').css('background-position', 'center -'+ $('img.load_img',me).attr('position') +'px ' );
	
	$active.text( parseInt($active.text()) + 1 );
	$switchTime = setTimeout("switchs()",3500);
	
	

//   $('.points b').mouseenter(function() {
   
   // b = $(this).attr('b');
   //	$('.news_line ul li').hide();
	//$('.news_line ul li').eq(b).show();
	//$('.points b').removeClass('act');
	//$('.points b').eq(b).addClass('act');
	//$('#active').text( parseInt(b) + 1 );
   
//   });	


}




// ======================================
// Insert at cursor
// ======================================

//myField accepts an object reference, myValue accepts the text strint to add 
function insertatcursor(myField, myValue) { 
//IE support 

if (document.selection) { 
myField.focus();

//with myValue 
sel = document.selection.createRange(); 
sel.text = myValue; 
}

//Mozilla/Firefox/Netscape 7+ support 
else if (myField.selectionStart || myField.selectionStart == '0') {

var startPos = myField.selectionStart; 
var endPos = myField.selectionEnd; 
myField.value = myField.value.substring(0, startPos)+ myValue+ myField.value.substring(endPos, myField.value.length); 
} else { 
myField.value += myValue; 
} 
}


// ======================================
// Get any selcte text
// ======================================

function getSelText() {
  var txt = '';
  if (window.getSelection) {
    txt = window.getSelection();
  } else if (document.getSelection) {
    txt = document.getSelection();
  } else if (document.selection) {
    txt = document.selection.createRange().text;
  } else return;
  //document.aform.selectedtext.value =  txt;
  return txt;
} 


$(document).ready(function(){


  // ======================================
  // Auto empty form fields  with .auto_empty   
  // ======================================
  $('.auto_empty').focus( function()
    {
      if (!$(this).data('done'))
      {
        $(this).data('done', true);
        $(this).val('');
      }
    });

  // ======================================
  // mailing list... Form
  // ======================================
  $('#mailing_list').submit(function()
    {
      form = $(this);
      if(form.data('progress')) { return false; }
      form.data('progress', true);
      action = form.attr('action');     // get form action
      data = form.serializeArray(); // get all form fields
	  $('.error_msg').removeClass('success error info warning');
	  $('.error_msg').text('جاري الارسال....').addClass('info');
	  //$('.error_msg').hide();
	  $('input[type=submit]' , form).attr('disabled','disabled');

      // Submit form 
      $.post(
        action, data,           
        function (e) {
        
          
			if( e == 1 ){
				  $('.error_msg').text('تم إضافة بياناتك').addClass('success');
				  
            }else if( e == 2 ){
                $('.error_msg').text('هذا البريد مضاف سابقاً').addClass('warning');

            }else if( e ==  3 ){
				$('.error_msg').text('البريد الإلكتروني غير صحيح').addClass('error');

			}else{				
				$('.error_msg').text('هناك مشكلة ، حاول مرة أخرى...').addClass('error');
			}

			form.removeData('progress');
			$('.error_msg').fadeIn();
			$('input[type=submit]' , form).attr('disabled','');
        },
        "json"
      ); // Ajax post


      return false;
      });
      

  // ======================================
  // Comment Form
  // ======================================
  $('.comment').submit(function()
    {
      form = $(this);

      // prevent duplicate submit  
      if(form.data('progress')) { return false; }
      form.data('progress', true);
      action = form.attr('action');     // get form action
      data = form.serializeArray(); // get all form fields
      
//      form.html('<img src="/css/img/lodings.gif" alt="Loading ...">');
//      form.html('loading...');
	  button = $('input[type=submit]', this);
	  button.val('Loading...').attr('disabled','disabled');
	  $('.error').hide();



      // Szubmit form 
      $.post(
        action, data,           
        function (data) {
          form.removeData('progress');

			if( data.success ){
				  form.hide();
				  $('.thanks').fadeIn();


			}else{				
				button.val('ارسل').attr('disabled','');
				if(data.error.comment) $('.er_comment').fadeIn();
				if(data.error.name) $('.er_name').fadeIn();
				if(data.error.article_id) $('.er_comment').fadeIn();
			}
	
        },
        "json"
      ); // Ajax post


      return false;
    });


  // ======================================
  // show commnt on page
  // ======================================
  $('.comment textarea').keyup( function()
    {
		last_li = $('.comments ul li').last();
		$('.comment',last_li).html( $(this).val().replace(/\n/g, "<br>"));
		last_li.show();

	});


  $('.comment input[name=name]').keyup( function()
    {
		last_li = $('.comments ul li').last();
		$('.name',last_li).html($(this).val());
		last_li.show();
	});
	










	
  // ======================================
  // vote4 functions
  // ======================================
  

  $('.vote_4of8 li').hover(function(){
	name = $('img',this).attr('title');
	$('.team_show_name').text(name);
  });
	
	
  $('.vote_4of8 input:checkbox').click(function(){
	me = $(this);
	
	
	var count = $('.vote_4of8 input:checked').length;
	if(count == 2 ) {
		if($('.vote_4of8 form').data('progress')) { return false; }
		$('.info_form').slideDown();
		}
	if(count == 3){
		if($('.vote_4of8 form').data('progress')) { return false; }
		alert('يسمح لك بإختيار فريقين فقط');
		me.attr('checked','');
		
		}
	
//    $("div").text(n + (n <= 1 ? " is" : " are") + " checked!");
	  
	});



  
  
  
  
  
  $('.vote_4of8 form').submit(function(){
    
      form = $(this);
	  email = $('input[name=email]',form).val();
//	  mobile = $('input[name=email]',form).val();


	  	var count = $('.vote_4of8 input:checked').length;
		if(count < 2 ){ alert('يجب عليك اختيار فريقين'); return false;}
		
        var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
		if(emailReg.test(email) == false || email == '') { alert('ادخل بريد  إلكتروني صحيح');	return false;}



      // prevent duplicate submit  
      if(form.data('progress')) { return false; }
	  
      form.data('progress', true);
      action = form.attr('action');     // get form action
      data = form.serializeArray(); // get all form fields
      
//      form.html('<img src="/css/img/lodings.gif" alt="Loading ...">');
//      form.html('loading...');
	  button = $('input:submit', this);
	  button.val('Loading...').attr('disabled','disabled');
	  $('.error').hide();



      // Szubmit form 
      $.post(
        action, data,           
        function (data) {
          form.removeData('progress');
		  

			if( data.success ){
				  //form.hide();
				  $('.info_form').hide();
				  $('.info_form_done').show();
				  //alert(data.success);
				  form.data('progress', true);
				  
		  	  	  $('.vote_4of8 input:checked').attr('disabled','disabled');


			}else{				
				button.val(' ارسل ').attr('disabled','');
//				if(data.error.comment) $('.er_comment').fadeIn();
//				if(data.error.name) $('.er_name').fadeIn();
//				if(data.error.article_id) $('.er_comment').fadeIn();
			}

        },
        "json"
      ); // Ajax post


	  //alert('Done');
	  
      return false;
	  
    });


  // ======================================
  // hide jop filds
  // ======================================
  $('#jop').change(function()
    {
        if( $(this).val() == 'yes' ){
            $('.jop_hide').show();
        }else{
            $('.jop_hide').hide();
            }
    });
	


}); // end ready jquery


 



  
