/*
    LovePowerAndSacrifice.com | lps.js
    
    Personally, I fink jQuery.com rulez. :)
    
    (C) JohnLlewellyn.com All Rights Reserved.
*/

// -- globals 
var Pages = new Array();
var count = 0;

// -- globals for form
var gttl = 0;
var s_qty = 0;
var n_qty = 0;
var n_total = 0;
var s_total = 0;
var post = 0;
var last_post = 0;

// -- add the hoverstate from a main navigation button
function AddNavHoverState ( elId ) { 
    
    // set state;
    $("li#" + elId + " img").hover(
        function(){ $(this).attr("src","/_assets/img/common/nav_" + elId + "_on.gif"); },
        function(){ $(this).attr("src","/_assets/img/common/nav_" + elId + "_off.gif"); }
    );
}

// -- removes the hoverstate from a main navigation button
function RemoveNavHoverState ( elId ) { 
                
    // remove nav link;
    var link =  $("li#" + elId + " a");      
    link.attr("href","#");
    link.attr("style","cursor:default;");
    
    // set image on;
    var img = $("li#" + elId + " img")
    img.unbind()
    img.attr("src","/_assets/img/common/nav_" + elId + "_on.gif");
}

// -- Pages Class;
var Page = function ( imgSrc , caption ) {
    this.ImgSrc = imgSrc;
    this.Caption = caption;
} 

// -- Just as easy to store data here;
function CreateData() {
     
     Pages[Pages.length] = new Page( "/_assets/img/spread/spread_0.jpg" , "" );
     Pages[Pages.length] = new Page( "/_assets/img/spread/new_spread.jpg" , "<p>In Sheffields City Hall the faithful feel the power of Jesus through the laying on of hands.</p>" );
     Pages[Pages.length] = new Page( "/_assets/img/spread/spread_2.jpg" , "<p>Right page:<br />City Hall, Sheffield.<br /><br />Left page:<br />Cornhill Manor, Northamptonshire. Believing they are touched by the Holy Spirit, worshippers may laugh, cry or speak in tongues.</p>" );
     Pages[Pages.length] = new Page( "/_assets/img/spread/spread_3.jpg" , "<p>Members from the Crown of Life community house travel weekly across South Yorkshire to visit people in their homes and hold group worship sessions.</p>" );
     Pages[Pages.length] = new Page( "/_assets/img/spread/spread_4.jpg" , "<p>Wooden cross at a ‘Celebrate Jesus’ Easter rally, held annually in a giant marquee at Cornhill Manor, Northamptonshire.</p>" );
}

function UpdateSpread() {
    
    // guards...
    if (count<0) { count=Pages.length-1 };
    if (count>Pages.length-1) { count = 0 };
    
    // update caption
    $("#caption img").attr("src","/_assets/img/spread/text_"+count+ ((count==1)?".jpg":".gif") );
    $("#caption p").html( Pages[count].Caption );
    $("#spreadImg img").attr("src", Pages[count].ImgSrc);
    
}



function resetForm() {
    gttl = 0;
    s_qty = 0;
    n_qty = 0;
    n_total = 0;
    s_total = 0;
    last_post = 0;
    $("#postage").val(last_post)
}

function checkTtls() {
    if ( n_qty < 0 ) n_qty = 0;
    if ( s_qty < 0 ) s_qty = 0;
    if ( s_total < 0 ) s_total = 0;
    if ( n_total < 0 ) n_total = 0;
    if ( gttl < 0 ) 
    if ( gttl < 0 ) gttl = 0; 
    if (( s_qty <= 0) && ( n_qty <= 0 )) resetForm();
}

function add(cost,type,qty){
    if ( type == "s" ) {
        s_qty += qty;
        s_total += cost;
    }
    if ( type == "n" ) {
        n_qty += qty;
        n_total += cost;
    }
    checkTtls();
    gttl += cost;
    updateForm();
}

function minus(cost,type,qty){
    if ( type == "s" ) {
        s_qty -= qty;
        s_total -= cost;
    }
    if ( type == "n" ) {
        n_qty -= qty;
        n_total -= cost;
    }
    checkTtls();
    gttl -= cost;
    updateForm();
}

function updateForm()
{
    checkTtls()
    $("#ctl00_MainContent_grandTtl").val( gttl.toFixed(2) ) 
    
    $("#ctl00_MainContent_signedBookTtl").val ( n_total.toFixed(2) );
    $("#ctl00_MainContent_signedBookQty").val ( n_qty );
    
    $("#ctl00_MainContent_signedBookSilverTtl").val ( s_total.toFixed(2) );
    $("#ctl00_MainContent_signedBookSilverQty").val ( s_qty );
    
    $("#ctl00_MainContent_ppTtl").val ( last_post.toFixed(2) );
}

function validateForm()
{
    if ( n_qty <= 0 && s_qty <= 0 ) { alert("Please place an order!"); return false; } 
    if ( last_post <= 0 ) { alert("Please choose your postage and packing!"); return false; }
    if ( $("#ctl00_MainContent__email").val() == "" ) { alert ("Please enter your email address!"); return false; }
    if ( $("#ctl00_MainContent__phoneno").val() == ""  ) { alert ("Please enter your phone number!"); return false; }
    if ( $("#ctl00_MainContent__address").val() == ""  ) { alert ("Please enter your address!"); return false; }
    
    $("#ctl00_MainContent_grandTtl").removeAttr("disabled"); 
    $("#ctl00_MainContent_signedBookTtl").removeAttr("disabled"); 
    $("#ctl00_MainContent_signedBookQty").removeAttr("disabled"); 
    $("#ctl00_MainContent_signedBookSilverTtl").removeAttr("disabled"); 
    $("#ctl00_MainContent_signedBookSilverQty").removeAttr("disabled"); 
    $("#ctl00_MainContent_ppTtl").removeAttr("disabled"); 
    $("#ctl00_MainContent_postage").removeAttr("disabled"); 
    
 }
 
 // -- kick of the js if the document is ready. 
function Go() {
    
    // nav setup 
    AddNavHoverState("introduction");
    AddNavHoverState("samplepages");
    AddNavHoverState("buythebook");
    
    // get the page id   
	var pageId = $('body').attr('class');
    
    // remove the hover for this page
    RemoveNavHoverState( pageId );
    
    // enter 
    if ( pageId == "enter" )
    {
        
        $("#enterSml").hover(
            function(){$(this).attr("src","/_assets/img/common/enter_on.gif")},
            function(){$(this).attr("src","/_assets/img/common/enter_off.gif")}
        );
        
        $("#enterLrg").hover(
            function(){$("#enterSml").attr("src","/_assets/img/common/enter_on.gif")},
            function(){$("#enterSml").attr("src","/_assets/img/common/enter_off.gif")}
        );
        
    }
    
    // spread
    if ( pageId == "samplepages" ) 
    {
    
        CreateData();
    
        $("#back").hover(
            function(){$(this).attr("src","/_assets/img/common/back_on.gif")},
            function(){$(this).attr("src","/_assets/img/common/back_off.gif")}         
        ).click(function(){
            count--;
            UpdateSpread();
        });
        
        $("#next").hover(
            function(){$(this).attr("src","/_assets/img/common/next_on.gif")},
            function(){$(this).attr("src","/_assets/img/common/next_off.gif")}         
        ).click(function(){
            count++;
            UpdateSpread();
        });
 
    }
    
    
    // book
    if ( pageId == "buythebook" ) 
    {
    
      updateForm()
    
      $("#minusNormalQty").click(function(){
            if ( n_qty > 0 ) {
                minus(12.99,"n",1)
            }
      });
      
      $("#plusNormalQty").click(function(){
            add(12.99,"n",1)
      });
    
      $("#minusSilverQty").click(function(){
            if ( s_qty > 0 ) {
                minus(155,"s",1)
            }
      });
      
      $("#plusSilverQty").click(function(){
            add(155,"s",1)
      });
 
      $("#ctl00_MainContent_postage").change(function(){
            // remove last post from total
            gttl -= last_post;
            
            // add new one
            gttl += parseFloat($(this).val());
            
            // set the last;
            last_post = parseFloat($(this).val());

            updateForm();
      })

    
 
    }

    
};

// for Safari
/*
if (/WebKit/i.test(navigator.userAgent)) { // sniff
  window.__load_timer = setInterval(function() {
      if (/loaded|complete/.test(document.readyState)) {
          Go(); // call the onload handler
      }
  }, 10);
}*/

 // for other browsers
 window.onload = Go;