var Dialog = {
		
		close : function(){
			$('#opacity_container').hide();
			$('#opacity_container .bw-content').html('');
		},
		
		init : function(){
				$('#opacity_container form').live( 'submit', Dialog.submit );
				$(document).keyup(function(e) {
					  if (e.keyCode == 27) {Dialog.close();}
				});
		},
		
		submit : function( e ){
			e.preventDefault();
			$.post($(this).attr('action')+'?send=true', 
			 $(this).serializeArray(), Dialog.postSubmit );
			 $('.bw-content').html( '<img alt="Loader" src="/static/img/layout/loader.gif" style="margin: 90px auto 0; display:block;" /><p style="text-align:center;">Loading...</p>' );
			 $('.bw-content').append('<span class="popupClose"></span>');
			
			return false;
		},
		
		postSubmit : function( data ){
			$('#opacity_container .bw-content').html( data );
                           $('.bw-content').append('<span class="popupClose"></span>');
		},
		
		contact: function(){
			$('#opacity_container .bw-content').load(
					'/index/contact',
					{},
					Dialog.showDialog
			);
			
		},
		
		recoverPin: function(){
			$('#opacity_container .bw-content').load(
					'/user/recoverpin',
					{},
					Dialog.showDialog
			);
			
		},
		
		changePin: function(){
			$('#opacity_container .bw-content').load(
					'/user/changepin',
					{},
					Dialog.showDialog
			);
			
		},
		
		showDialog: function( content ){
			if( content == '#notlogged#' ){
				return document.location.href = "/user/login";
			}
			$('#opacity_container').show();
                           $('html, body').animate({scrollTop:0}, 'slow'); 
                           $('.bw-content').append('<span class="popupClose"></span>');
		},
		
		closeDialog : function(){
			$('#opacity_container').hide();
		}
};

$(document).ready(function() {
 
//align btnV1
$('.w_btn .btnV1, .b-w_content .btnV1').each(function(){
      $(this).css({'position': 'absolute', 'bottom': '0', 'right': '0'});
});
//align btnV2
$('.w_btn .btnV2, .b-w_content .btnV2').each(function(){
   lengthBtn1 = $(this).parent().children('.btnV1').length;
   if (lengthBtn1 == 0)
      $(this).css({'position': 'absolute', 'bottom': '0', 'right': '0'});
   else
      $(this).css({'position': 'absolute', 'bottom': '40px', 'right': '0'});
});
//align btnV3
$('.w_btn .btnV3, .b-w_content .btnV3').each(function(){
   lengthBtn1 = $(this).parent().children('.btnV1').length;
   lengthBtn2 = $(this).parent().children('.btnV2').length;
   if ((lengthBtn1 + lengthBtn2) == 1)
      $(this).css({'position': 'absolute', 'bottom': '40px', 'right': '0'});
   else if ((lengthBtn1 + lengthBtn2) == 2 )
      $(this).css({'position': 'absolute', 'bottom': '80px', 'right': '0'});
   else
      $(this).css({'position': 'absolute', 'bottom': '0', 'right': '0'});
});
//sidebar fix
$('.b-w_content .btn_v2, .b-w_content .btn_v1').each(function(){
   $(this).css({'right': '12px'});
});
$('.b-w_content .btn_v2, .b-w_content .btn_v1').each(function(){
   $(this).css({'right': '12px'});
});
$('.b-w_content .btn_v2').each(function(){
   $(this).css({'margin-bottom': '11px'});
});

$('#messageSended').parent('.bw-content').css('min-height','');

//clouse popup button
$('.popupClose').live('click', function() {$('#opacity_container').hide();});

   //select choise your island
   $('#choise_your_island').click(function() {
      $('#choise_your_island .islands').slideDown();
   });
   //hide select choise your island
  $('#choise_your_island .islands').mouseleave(function() {
    $(this).slideUp();
  });

   //hidden password text
   $('#password-txt').click(function() {
      $(this).hide();
      $('#my_pin').focus();
   });
   $('#my_pin').focus(function() {
      $('#password-txt').hide();
   });
   $('#password-txt-2').click(function() {
      $(this).hide();
      $('#my_pin-2').focus();
   });
   $('#my_pin-2').focus(function() {
      $('#password-txt-2').hide();
   });

   //clear input
    $(".cls").click(function () {
         $(this).val('');
       });

    $('.cls-font').focus(function() {
      $(this).removeClass('cls-font');
    });


    //checkbox
      $('#male').click(function() {
         $(this).addClass('checked');
         $('#female').removeClass('checked');
         $('#sex').val('M');
      });

      $('#female').click(function() {
         $(this).addClass('checked');
         $('#male').removeClass('checked');
         $('#sex').val('F');
      });

      $('#own_car_yes').click(function() {
         $(this).addClass('checked');
         $('#own_car_no').removeClass('checked');
         $('#own_car').val(1);
      });
      
      $('#own_car_no').click(function() {
         $(this).addClass('checked');
         $('#own_car_yes').removeClass('checked');
         $('#own_car').val(0);
      });

       $('#own_house_yes').click(function() {
         $(this).addClass('checked');
         $('#own_house_no').removeClass('checked');
         $('#own_house').val(1);
      });

      $('#own_house_no').click(function() {
         $(this).addClass('checked');
         $('#own_house_yes').removeClass('checked');
         $('#own_house').val(0);
      });

      $('#credit_card_yes').click(function() {
         $(this).addClass('checked');
         $('#credit_card_no').removeClass('checked');
         $('#credit_card').val(1);
      });

      $('#credit_card_no').click(function() {
         $(this).addClass('checked');
         $('#credit_card_yes').removeClass('checked');
         $('#credit_card').val(0);
      });

      $('#mailing_yes').click(function() {
         $(this).addClass('checked');
         $('#mailing_no').removeClass('checked');
         $('#mailing').val('Y');
      });

      $('#mailing_no').click(function() {
         $(this).addClass('checked');
         $('#mailing_yes').removeClass('checked');
         $('#mailing').val('N');
      });

      $('#agree_checkbox').click(function() {
         var agree = $('#agree').val();
         
         if (agree == 1) {
               agree_val = 0;
               $(this).removeClass('checked');
            }else{
               agree_val = 1;
               $(this).addClass('checked');
          }
         $('#agree').val(agree_val);
      });
      
      $('#clique span').click( 
    		  function(){
    			  id = parseInt( $(this).attr('id').replace('cl_','') );
    			  $('#images.slider img').hide();
    			  $('#images.slider img#sliderImage'+(id)+'').show();
    		  }
      );
      
      if(  $('#clique').length > 0 ){
	      id = 2;
	      autoChange = function(){
	    	  while( $('#clique span#cl_'+id ).length == 0 ){
	    		  id++;
	    		  if( id > 4 ) id = 1;
	    	  }
	    	  
	    	  $('#clique span#cl_'+id ).trigger( 'click' );
	    	  id++;
	      };
	      
	      setInterval( autoChange, 4000 );
      }
      $('.people_more').click(
    		  function(){
    			  $('.hiddenPeople').show();
    			  $(this).remove();
    		  }
      );
      
      $('#moreNews').click(
    		  function(){
    			  $('.hiddenNews').show();
    			  $(this).remove();
    		  }
      );
      
      $('.moreTreats a').click(
    		  function(){
    			  $('#treats2content .more').removeClass('more');
    			  $(this).parent().remove();
    		  }
      );
      
      $faq = null;
      $('#faq .question').click(
    		  function(){
    			  if( $faq == null ||  $(this) != $faq ){
	    			  $( '.answer' ).hide();
	    			  $(this).parent().find('.answer').fadeIn();
	    			  $faq = $(this);
    			  }
    				  
    		  }
      );

      $('.btn_print').click(
    		  function(){
    			  $('.text_page').printElement({printMode:'popup'});
                             $('.newsPrint').printElement({printMode:'popup'});
    			  return false;
    		  }
      );
      
      Dialog.init();
      
      $('.statement .btnNext').click( function(){ $('.clear').html(''); changePage( 1 ); } );
      $('.statement .btnPrev').click( function(){ changePage( -1 ); } );
      
      function changePage ( dir ){
		page = $('.statement table.tab_v1 tr:visible:last').attr('class');
		
		$('.'+page).hide();
		pageEl = page.split('-');
		page = 'page-'+ (parseInt( pageEl[1] )+dir);
		$('.'+page ).show();

		if( page == 'page-1'  ){
			 $('.statement .btnPrev').hide();
		}else{
			$('.statement .btnPrev').show();
		}
		
		if( page == $('.statement table.tab_v1 tr:last').attr('class') ){
			$('.statement .btnNext').hide();
		}else{
			$('.statement .btnNext').show();
		}
	}
	
	$('.treats .buttons span').click( function(){
		$this = $(this);
		$('.t-content').addClass('more');
		$( '.'+$this.attr('class')+'_cnt', $this.parent().parent()).removeClass('more');
	} );
	
	$('.redeemImage').each(function() {
		 $(this).mouseenter(function(){
		 $(this).parent().find('.cloudBox').fadeIn(10);
		 }).mouseleave(function(){
		 $(this).parent().find('.cloudBox').fadeOut(10);
		 });
		 });

	$('.redeem-table .controlls span').click(
		function(){
			id = $(this).parent().attr('id');
			
			if( $(this).hasClass('up') ){
				$prev = $( '#'+id+'_items .redeemItem:not(.more):first' ).prev();
				if( $prev.length == 1 ){
					$( '#'+id+'_items .redeemItem:not(.more):last' ).addClass('more');
		                        $prev.removeClass('more');
				}
			}else{
                                    $next = $( '#'+id+'_items .redeemItem:not(.more):last' ).next();
				if( $next.length == 1 ){
					$next.removeClass('more');
		                        $( '#'+id+'_items .redeemItem:not(.more):first' ).addClass('more');
				}

			}
		}
	);

});

profileSelectSetup = function(){
	$(document).ready(function() {
		$("form").relatedSelects({
			onChangeLoad: '/user/getrelated/',
			selects: [ 'Eiland', 'Areaid', 'StreetID']
		});
	});
};


