$(function(){

	$('#mainmenu span').mouseover(function(){
									$(this).children('div').show();
									$(this).children('a').addClass('hover');
								});

	$('#mainmenu span').mouseout(function(){
									$(this).children('div').hide();
									$(this).children('a').removeClass('hover');
								});
								
	$('#tech-section').click(function(){ return false; });
	
	$('#popupClose').click(function(){
										hidePopup();
									});
									
	$('#popupImg img.main').click(function(){
											hidePopup();
										});
  
  // close seo
  toggle($('#seo').find('#backendBoxExpandText'));

});

function hidePopup(){
	$('#popup').fadeOut();
}

function showPopup(img, showForm, id){
	$('#popupBorder').hide();
	$('#popupImg').hide();
	$('#popup').show();
	
	if(showForm){
		$('#popupForm').show();
		$('#popupImg').hide();
		$('#popup').css('margin-left', '-390px');
		$('#popupBorder').fadeIn("slow");     
    $('#popupTitleID').text('Tell a Friend about this Special!');     
    $('#offer_id').val(id);
    $('#friendForm').show();
    $('#friendForm').unbind('submit');
    $('#friends_email').parent().find('label').css('color', '#ffee00');      
    $('#your_email').parent().find('label').css('color', '#ffee00');      
    $('#friendForm').bind('submit', function() {
    
      $.post('postFriendForm', $('#friendForm').serialize(), function(data) {
      
        if ( data == 'OK' ) {
        
        $('#popupTitleID').html('Offer sent to your friend');
        $('#friendForm').hide();
      
        } else {
        
          $('#friends_email').parent().find('label').css('color', '#ffee00');      
          $('#your_email').parent().find('label').css('color', '#ffee00');
        
          if ( data == 'NOK1' )
            $('#friends_email').parent().find('label').css('color', 'red');
          else
            $('#your_email').parent().find('label').css('color', 'red');
          
        }
        
      });
      
      return false;
    
    
    });
    
	}
	else {
		$('#popupForm').hide();
		$('#popupImg').show();
		$('#popupImg img.main').hide();
		$('#popupBorder').fadeIn("slow");
		
		$('#popupImg img.main').attr('src', img);
		
		$('#popupImg img.main').load(function(){
												//IE won't give a width of a hidden object
												$(this).css('top', '200%');
												$(this).show();
												var w = $(this).attr('width');
												var h = $(this).attr('height');
												$(this).hide();
												$(this).css('top', '0px');
												var m = w/2 + 110;
												
												$('#popup').css('margin-left', -m+'px');
												$('#popupImg').animate({'width' : w, 'height' : h}, "slow", function() { $('#popupImg img.main').fadeIn(); });
											}); 
	}
}

function toggle(obj) {

  $(obj).parent().parent().parent().find('.backendBoxContent').toggle();
  
  // border
  if ( $(obj).parent().parent().parent().find('.backendBoxContent').css('display') == 'none' ) {
  
  $(obj).parent().parent().parent().find('.viewPage').css('display', 'none');
  
    // remove border
    $(obj).parent().parent().css('borderBottom', '0px');
  
    // show expand
    $(obj).parent().parent().find('#backendBoxExpandText').text('Expand');
  
     // img flip
    $(obj).parent().parent().find('#backendBoxExpandImage').attr('src', 'images/backend_expand.png');
 
  } else {
   
  $(obj).parent().parent().parent().find('.viewPage').css('display', 'block');

    // add border
    $(obj).parent().parent().css('borderBottom', '1px solid #666');     

    // show collapse
    $(obj).parent().parent().find('#backendBoxExpandText').text('Close');
    
    // img flip
    $(obj).parent().parent().find('#backendBoxExpandImage').attr('src', 'images/backend_collapse.png');
 
  }
      
}
