
height = 255;
width = 252;
offset = 50;
offsetpx = height - offset + "px";
heightpx = height + 'px';
widthpx = width + 'px';
path = '../../';

$(document).ready(function(){
    var myStatus = false;
                
    
	var pub_class = document.getElementById('maincontent').getAttribute('class');
	
	if(pub_class == 'publication_reprint' || ($.browser.msie && parseInt($.browser.version) == 7 )){
		
		
		var popup_html = "<div id=\"form_wrapper\">";
		var popup_html = popup_html + "<div id=\"form_mask\"></div>";
		var popup_html = popup_html + "<div id=\"form_holder\">";
		var popup_html = popup_html + "<span class='headGreenBold'>Publication Re-print Request for</span><br/>";
		var popup_html = popup_html + "<p></p>";
		var popup_html = popup_html + "<input type='text' id='name_pdf' name='name' value='name' class='input' /><br />";
		var popup_html = popup_html + "<input type='text' id='email_pdf' name='email' value='email' class='input' /><br />";
		var popup_html = popup_html + "<input type='text' id='company_pdf' name='company' value='company' class='input' /><br />";
		var popup_html = popup_html + "<input type='text' id='telephone_pdf' name='telephone number' value='telephone number' class='input' /><br />";
		var popup_html = popup_html + "<textarea name='request' id='request_pdf' class='input textarea textrequest'>request</textarea>";
		var popup_html = popup_html + "&nbsp; <br/>";
		var popup_html = popup_html + "<button type=\"button\" id='news_close'>Cancel</button>";
		var popup_html = popup_html + "<button type=\"submit\" id='news_submit'>Submit Request</button>";
		var popup_html = popup_html + "&nbsp; <br/>";
		var popup_html = popup_html + "</div>";
		var popup_html = popup_html + "</div>";
		
		
		
		
		
		if($.browser.msie && parseInt($.browser.version) == 7 ){
			
		
			var ie7_dump = $('body').html();
			alert ($('body').html());
			ie7_dump = ie7_dump + popup_html;
		
			$('body').html(ie7_dump);
		
			$('#form_holder').css('margin-top', '500px;');			
		}else{
			
			
			$('body').append(popup_html);	
			
		}
		
		
		
		
		  function show_ln_sub_form(){
			   
			   $('#form_wrapper').css('display' , 'block');
			    
		  }
		  
		  
		  function close_ln_sub_form(){
			   
			   $('#form_wrapper').css('display' , 'none');
			   	
		  }
		  
		  
	 $('#news_close').click(function(event){
			   
			   event.preventDefault();
			   close_ln_sub_form();
			   return false;
		  });
	 
	  $('#form_mask').click(function(event){
			   
				event.preventDefault();
			   close_ln_sub_form();
			   return false;
		  });
	  
	  $('div.publication_reprint a').click(function(e){
		  
		  e.preventDefault();
		  
		  var link_text = $(this).html();
		  
		  $('#form_holder').find('p').empty().html(link_text);
		  
		  show_ln_sub_form();
		return false;  
	 });
		
		
		
	}
	
	
	
	$('.showHide').click(
        function() {
            if(!myStatus){
                //Open
                $('.menuDrop').slideToggle('slow', '-1px');
                $('#stage2').show();
                $('#stage1').hide();
                myStatus = true;
				
            }else{
                //close
                $('.menuDrop').slideToggle('slow', '-205px');
                $('#stage1').show();
                $('#stage2').hide();
				
                setTimeout(function() {myStatus = false;}, 600);
            }
            return false;
        }
    );
    
    $('.open').hover(
        function (){
            if(!myStatus){
                //Open
                $('.menuDrop').slideToggle('slow', '-1px');
                $('#stage2').show();
                $('#stage1').hide();
                setTimeout(function() {myStatus = true;}, 600);
            }
        }, function(){
            //do nothing
        }
    )
    
    
    $('#submit').click(function(){
        
     
        //Both email and phone number are blank
        if( ($('#email').attr('value') == 'email' || $('#email').attr('value') == '') &&
           ($('#telephone').attr('value') == 'telephone number' || $('#telephone').attr('value') == '') ){
            alert('Please enter an email address or telephone number so we may contact you.');
            return false;
        }
        
        $('#stage2 input').each( function(i){
        //Search for the name=value match
            if( $(this).attr('name') == $(this).attr('value').replace(' ', '_') ){
                $(this).attr({'value' : ''}); //Clear value if it's the default value
            }
        });
        if( $('#query').attr('name') == $('#query').attr('value')){
            $('#query').attr({'value' : ''});
        }
        
        
        
        //alert('submit form');
        $('#stage3').show();
        $('#stage2').hide();
        $('.menuDrop').slideToggle('slow', offsetpx);

             
        //Grab data
        var dataString = 'name='+ $('#name').attr('value') +
                         '&email=' + $('#email').attr('value') +
                         '&company=' + $('#company').attr('value') +
                         '&telephone=' + $('#telephone').attr('value') +
                         '&query=' + $('#query').attr('value') +
                         '&page=' + document.title;
                         ;
        
        //Send data
        $.ajax({  
            type: "POST",  
            url: "" + path + "_dropdown/process.php",
            data: dataString,  
            success: function(html) {
                //alert(html); debug what's happening in an alert
                               
                
                setTimeout(function(){
                    //hide permanently
                    $('.menuDrop').slideToggle('slow', '-' + height + 'px' );
               }, 1500);
            }
        });  
        
        
       return false;
       
    });
    
    
	$('#news_submit').click(function(){
        
     
        //Both email and phone number are blank
        if( ($('#email_pdf').attr('value') == 'email' || $('#email_pdf').attr('value') == '') &&
           ($('#telephone_pdf').attr('value') == 'telephone number' || $('#telephone_pdf').attr('value') == '') ){
            alert('Please enter an email address or telephone number so we may contact you.');
            return false;
        }
        
        $('#form_holder input').each( function(i){
        //Search for the name=value match
            if( $(this).attr('name') == $(this).attr('value').replace(' ', '_') ){
                $(this).attr({'value' : ''}); //Clear value if it's the default value
            }
        });
        if( $('#request').attr('name') == $('#request').attr('value')){
            $('#request').attr({'value' : ''});
        }
        
        //Grab data
        var dataString = 'name='+ $('#name_pdf').attr('value') +
                         '&email=' + $('#email_pdf').attr('value') +
                         '&company=' + $('#company_pdf').attr('value') +
                         '&telephone=' + $('#telephone_pdf').attr('value') +
                         '&query=' + $('#request_pdf').attr('value') +
						 '&document=' + escape($('#form_holder').find('p').html()) +
                         '&page=' + document.title;
                         ;
        
        //Send data
        $.ajax({  
            type: "POST",  
            url: "" + path + "_dropdown/request_process.php",
            data: dataString,  
            success: function(html) {
                alert('Request Sent.');
				//alert(html);
                               
                
                setTimeout(function(){
                
				  $('#form_wrapper').css('display' , 'none');
				
				
               }, 3000);
            }
        });  
        
        
       return false;
       
    });
	
	
	jQuery.fn.slideToggle = function(speed, heightX, easing, callback) {
        return this.animate( {marginTop: heightX} , speed, easing, callback);  
    };
    
    $(document).pngFix();
    
    $('div.stage').css({'z-index' : '1'});
    $('#stage2').hide();
    $('#stage3').hide();
    
    
    $('.input').focus(function(){
        if($(this).attr('value') == $(this).attr('name') ||
           $(this).attr('value').replace(' ', '_') == $(this).attr('name')
        ){
            $(this).attr({'value' : ''});
            $(this).css({'color' : '#333', 'text-align' : 'left'});
            //Grab the info and send it as a post to sMail.php
        }
    });
    
    $('.input').blur(function(){
        if( $(this).attr('value') == ''){
            var curValue =  $(this).attr('name').replace('_', ' ');
            $(this).attr({'value' : curValue}); //asign its own name
            $(this).css({'color' : '#AAA', 'text-align' : 'center'});
        }
        //otherwise leave field as is.
    });
    $('.textarea').attr({'value' : 'query'});
	$('.textrequest').attr({'value' : 'request'});
    
    $('.menuDrop').css({'display' : 'block'});
});
