jQuery(document).ready( function($) {

     /* Facebox plugin call */
     $("a[rel*=facebox]").facebox();
     $('#facebox').livequery(function() {
       $(this).bgiframe();
    });
     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( '' , 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( '' , 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( '' , 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( '' , cat , pieces[1] );
     })

	$("div.btn-container > a.btn-anchor").attr("href","#")	;
     // if submit link clicked
     $("div.btn-container > a.btn-anchor").bind("click", function(){
         // get title of the package
         var pck_name = "";
         pck_name = $(this).parents("div.price-pack").siblings("div.content-pack").find("div.title:first").text();
			//alert(pck_name);
         //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 && pck_name != "" ){
            //submit the form.
            //alert($("form[name='_hform']").attr('action'));
            //$("form[name='_hform']").submit();
            var this_url = location.href;
            var arr = this_url.split("/");
            var new_url = "";
            for(var k = 0; k<arr.length-3;k++)new_url += arr[k]+"/";
            new_url += arr[k];
            pck_name = pck_name.replace(/\,/,"+");
            document._hform.action = new_url + "/" + urlencode(pck_name);//alert(document._hform.action);
            document._hform.submit();

         }
     });
	 
	 $("div.btn-container_int > a.btn-anchor").attr("href","#")	;
     // if submit link clicked
     $("div.btn-container_int > a.btn-anchor").bind("click", function(){
         // get title of the package
         var pck_name = "";
         pck_name = $(this).parents("div.price-pack").siblings("div.content-pack").find("div.title:first").text();
         //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(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 && pck_name != "" ){
            //submit the form.
            //alert($("form[name='_hform']").attr('action'));
            //$("form[name='_hform']").submit();
            var this_url = location.href;
            var arr = this_url.split("/");
			var new_url = "";
            for(var k = 0; k<arr.length-3;k++)new_url += arr[k]+"/";
            new_url += arr[k];
            pck_name = pck_name.replace(/\,/,"+");
            document._hform.action = new_url + "/" + urlencode(pck_name);//alert(document._hform.action);
            document._hform.submit();

         }
     });
	 
	 

     function urlencode( url )
     {
        url = url.replace(/ /g,"+");
        url = url.replace(/#/, "");
        //alert(url);
        return url;
     }

     page_id_to_load = get_page_id_from_url();

     // 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,
           current_page: page_id_to_load
       }
    );

    loadPackages(page_id_to_load);

    intro = intro || '';
   // $("div.text-pack").html(intro);
   $("div.pic-left-pack").css("width","50px");
   
    $("a.contact_links").attr("href", "javascript:void(0);");
    
    $("a.sendquery").bind("click",function(){
    				var title = $(this).parent().parent().parent().parent().children("div.content-pack").children("div.title").clone();
    				//title.children("img").remove();
    				var pckname = jQuery.trim(title.html()) +" ("+myPckCityCountry+")";
    				$("input[type='hidden'][name='query_pckname']").val(pckname);   				
    				$(".msg").html("");
    				$("textarea[name='remarks']").val("");
    				//$("#sendqueryfrm").bgiframe();
    				$(".sendquerycontent").css("background-color", "#FFF");    				
					$("#sendqueryfrm").show().dialog( {resizable:true,width:'700px',height:'405px',draggable:true,position:['center','middle'],modal:true,overlay:{ background: '#000', opacity: 0.5 } } );
					return false;
				});
				
	$("img#emailformbtnfield").css("cursor", "pointer").bind("click", function(){
		
		$.ajax({
            type:"POST",
            url:"sendmail.php",
            data:"name="+$("input[type='text'][name='name']").val()+"&email="+$("input[type='text'][name='email']").val()+"&tel="+$("input[type='text'][name='tel']").val()+"&mob="+$("input[type='text'][name='mob']").val()+"&remarks="+$("textarea[name='remarks']").val()+"&packagename="+$("input[type='hidden'][name='query_pckname']").val()+"&dom_int="+$("input[type='hidden'][name='mydom_int']").val(),
            success:function(msg){
            	$("div.msg").html("*"+msg);
        	}
    	});
        return false;
   });

}); 
//end $(document).ready()


function get_page_id_from_url()
{
	this_url = location.href;
    arr = this_url.split("/");
    len = arr.length;
    page_id_to_load = 0;

    if( arr[len-2] == "page" )
    {
       page_id_to_load = parseInt( arr[len-1]) - 1 ;
    }
    return page_id_to_load;
}

function initialloadPackages( page_id , jq  )
{
    $("div.pagination-package").hide();
    page_id = parseInt( page_id );
    $("div#"+(page_id + 1 )).show();
}

function loadPackages( page_id , jq )
{
    $("div.pagination-package").hide();
    page_id = parseInt( page_id );

    if( jq )
    {
      this_url = location.href;
      arr = this_url.split("/");
      arr[arr.length-1] = page_id + 1;
      new_url = "";
      for(var k =0; k<( arr.length - 1);k++)new_url += arr[k] + "/";
      new_url += arr[k] ;
      new_url = new_url.replace(/#/g,"");
      window.location.href =  new_url;
    }
    else
    $("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+":"+pck);
  //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 = '';
  $("#"+ cat + "-" + pck ).show();   
}

function ChatPopUp(email)
{
	//email=prompt("Please enter your email address","");
 window.open("http://203.76.139.6/Chat/Non_Air.asp?Email="+email,"","scrollbars=yes,width=480,height=400,resizable=no");
 return false;
}