jQuery(document).ready( function($) {

     /* Facebox plugin call */
     $("a[rel*=facebox]").facebox();
     
     if( $( "input[type='radio'][class='categoryradio']" ).length )
     {
         //iterate over all the dropdowns with the name of flycityselect
         $( "input[type='radio'][class='categoryradio']" ).each(function(){
              //get the id of the select
              id = this.id;
              //and retrieve the index of the package  from id
              pieces = id.split("-");
              //get the city name that select currently contains
              var city = '';
              //using the package index, get the category radios for this package and retrieve the currently selected  category
              var cat = $("input[id='categoryradio-"+ pieces[1] +"']:checked").val();
              //make a call to showPrice() passing the city and category
              showPrice( city , cat, pieces[1] );
         });    
     }
     
     if( $( "select[name='flycityselect']" ).length )
     {
         //iterate over all the dropdowns with the name of flycityselect
         $( "select[name='flycityselect']" ).each(function(){
              //get the id of the select
              id = this.id;
              //and retrieve the index of the package  from id
              pieces = id.split("-");
              //get the city name that select currently contains
              var city = this.value;
              //using the package index, get the category radios for this package and retrieve the currently selected  category
              var cat = $("input[id='categoryradio-"+ pieces[1] +"']:checked").val();
              //make a call to showPrice() passing the city and category
              showPrice( city , cat, pieces[1] );
              
         });
     }

    //when a radio with name of 'categoryradio' is clicked on
     $("input[class='categoryradio']").click( function(){
         //get the id of the clicked radio
         var id = $(this).attr('id');
         //and retrieve the index of the package  from id
         pieces = id.split("-");
         //get the city name that the select corresponding to package index contains
         var city = $( "select[id='flycityselect-"+ pieces[1] +"']" ).val();
         //get the category from the clicked radio
         var cat = $(this).val();
         //make a call to showPrice() passing the city and category
         showPrice( city , cat, pieces[1] );
     })

     //when the value of a select with the name of flycityselect is changed
     $("select[name='flycityselect']").change(function(){
         //get teh id of the select
         id = $(this).attr('id');
         //and retrieve the index of the package  from id
         pieces = id.split("-");
         //get the city name that select currently holds
         var city = $(this).val();
         //using the package index, get the category radios for this package and retrieve the currently selected  category
         var cat = $("input[id='categoryradio-"+ pieces[1] +"']:checked").val();
         //make a call to showPrice() passing the city and category
         showPrice( city , cat , pieces[1] );
     })


     // if submit link clicked
     $(".btn-anchor").click(function(){ 
         //get the id of the clicked link that also represents the index number of the package
         var pck = $(this).attr('id').substr(3);
         //retrieve the value of the city for this package at the time the link was clicked
         var city = $( "#flycityselect-" + pck ).val();
         if(city == undefined ) city = '';
         //retrieve the value of the category selected
         var cat = $( "input[id='categoryradio-" + pck + "']:checked" ).val();

         if( cat == undefined ) cat = 'cat';
         //put the hidden field value to the one formed by city and cat
         $("input[type='hidden'][name='city-cat']").val( city + "-" + cat );
         //update the hidden field value to package index
         $("input[type='hidden'][name='pckindex']").val(pck);
         //if hidden field value has been set
         if( $("input[type='hidden'][name='city-cat']").val().length > 0 && $("input[type='hidden'][name='pckindex']").val().length > 0 ){
            //submit the form. 
            $("form[name='_hform']").submit();
         }
     });

     // pagination initialize
     $("#pagination").pagination(
       $("div.pack").length,{
           items_per_page:5,
           num_display_entries:4,
           next_text:"Next",
           prev_text:"Previous",
           num_edge_entries:1,
           ellipse_text:'...',
           next_show_always:false,
           prev_show_always:false,
           callback:loadPackages
       }
    );

    loadPackages(0);

    intro = intro || '';
    $("div.text-pack").html(intro);

}); 
//end $(document).ready()


function loadPackages( page_id , jq )
{   
    $("div.pagination-package").hide();
    $("div#"+(page_id + 1 )).show();
}

/* this method sets the visibility of the div that contains price for the specified city and category */
function showPrice( city , cat , pck ){
  //alert(city + ":" + cat);
  //Hide all the price divs

  //$(".city-category-price").hide();
  
  $(".city-category-price[name='pck"+pck+"']").hide();
  //make visible only the intended price div
  if(city == undefined ) city = '';
  if(cat == undefined)cat = '';
  $("#" + city + "-" + cat + "-" + pck ).show();   
}
