jQuery(document).ready( function($) {
      //alert($(".intro-text").height());
      //$(".inner-container").css('top',$(".intro-text").height()) ;
     /* Facebox plugin call */
     $("a[rel*=facebox]").facebox();
     
     $("div.category").each(function(){
          //get the id of the select
          id = this.id;
          //and retrieve the index of the package  from id
          pieces = id.split("-");
          
          if( $("#flycityselect-"+pieces[1]).length ){
              this.style.display = "none";
          }
     });
     
     $("select[name='flycityselect']").each( function(){
         hub = this.value;
         //get the id of the select
         id = this.id;
         //and retrieve the index of the package  from id
         pieces = id.split("-");
         
         $("#"+hub+"-"+pieces[1]).show();
         
     })
     
     
     if( $( "input[type='radio'][class='categoryradio']" ).length )
     {
         //iterate over all the dropdowns with the name of flycityselect
         $( "input[type='radio'][class='categoryradio']" ).each(function(){

             if(this.checked)
             {
                //get the id of the select
                id = this.id;
                //and retrieve the index of the package  from id
                pieces = id.split("-");

                package_list_item = pieces[1];

                //get the city name that select currently contains
                var city = null;
                if( $( "select[name='flycityselect']" ).length )
                {
                    city = $( "select[id='flycityselect-"+ package_list_item +"']" ).val();
                }
                //using the package index, get the category radios for this package and retrieve the currently selected  category
                var cat = $("input[id='categoryradio-"+ package_list_item +"']:checked").val();
                //make a call to showPrice() passing the city and category
                showPrice( city , cat, package_list_item );
             }
         });    
     }
     
     if( $( "select[name='flycityselect']" ).length )
     {
         //iterate over all the dropdowns with the name of flycityselect
         $( "select[name='flycityselect']" ).each(function(){

             if( this.selected)
             {
                //get the id of the select
                id = this.id;
                //and retrieve the index of the package  from id
                pieces = id.split("-");

                package_list_item = pieces[1];

                var cat = null;
                //using the package index, get the category radios for this package and retrieve the currently selected  category
                if( $( "input[type='radio'][class='categoryradio']" ).length )
                {
                    cat = $("input[id='categoryradio-"+ package_list_item +"']:checked").val();
                }
                //get the city name that select currently contains
                var city = this.value;

                //make a call to showPrice() passing the city and category
                showPrice( city , cat, package_list_item );
             }
              
         });
     }

     //alert(  );
     //.find("input[type='hidden'][name='package_id']").val( parents(".pack") ) ;
     packages_nohub_nocategory = $(".pack:not(:has(.categoryradio,select[name='flycityselect']))");
     for( k = 0; k < packages_nohub_nocategory.length ; k++ ){
         id = packages_nohub_nocategory.get(k).id;
         //and retrieve the index of the package  from id
         pieces = id.split("-");
         pck = pieces[1];
         packages_nohub_nocategory.find("input[type='hidden'][name='package_id']").val( mapping[pck] );
     }
     

    //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
         city = null;
         if( $( "select[name='flycityselect']" ).length )
         {
            city = $( "select[id='flycityselect-"+ pieces[1] +"']" ).val();
         }
         //get the category from the clicked radio
         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
         city = $(this).val();
         
         $(this).parent(".flycity").siblings(".category").hide();
         $("#"+city+"-"+pieces[1]).show();
         
         cat = null;
         if( $( "#"+city+"-"+pieces[1]+" input[type='radio'][class='categoryradio']" ).length )
         {
            //using the package index, get the category radios for this package and retrieve the currently selected  category
            cat = $("#"+city+"-"+pieces[1]+" 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. 
            //alert($("form[name='_hform']").attr('action'));
            //$("form[name='_hform']").submit();
            document._hform.submit();

         }
     });

     $("div.submit-button-container input[type='image']").click(function(){

       package_id_holder = $(this).parent().siblings("input[type='hidden'][name='package_id']");
       package_id = package_id_holder.val();
       if( package_id.length == 0 ) return false;
       $.ajax({
          type:"POST",
          url:"extra/ajax.package_name.php",
          data:"pid=" + package_id ,
          success:function( data , status ){
            if( status == "success" && data.length > 0 )
            {
                package_id_form =  package_id_holder.parent();
                action = package_id_form.attr( 'action' );
                //action_pieces = action.split("?");
                querystring =  ( action.substr( action.indexOf("?") + 1 ) ).split("&") ;
                action = "";
                for( parameter in querystring ){
		           // action += "/" + escape( querystring[parameter].substr( querystring[parameter].indexOf("=")+1 ) );
                   // if( parameter == 0 )action += "/pilgrims";

                }
                action += escape( querystring[1].substr( querystring[1].indexOf("=")+1 ) );
                action += "/"+escape( data );
                //alert( action );
                package_id_form.attr('action', action );
                //package_id_form.attr( 'action' , package_id_form.attr('action')+"&packagename="+ escape( data ) );
                //alert(package_id_form.attr('action'));
                package_id_form.submit();
            }
          }
       });
         return false;
       });

     // 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);
    
   

    $(".itinerary" ).click(function(){
        jQuery.facebox( "<img src=webroot/js/lib/facebox/loading.gif" );
        id = $(this).attr('id');
        pieces = id.split("-");
        pck = $("form[name='form-"+pieces[1]+"'] input[name='package_id'][type='hidden']").val();
        $("#itinerary-content").load('files/'+ pck + '/itinerary.html',null,function(){
            jQuery.facebox( $("#itinerary-content").html() );
        });
        
    });
    
    $(".inex" ).click(function(){
        jQuery.facebox( "<img src=webroot/js/lib/facebox/loading.gif" );
        id = $(this).attr('id');
        pieces = id.split("-");
        pck = $("form[name='form-"+pieces[1]+"'] input[name='package_id'][type='hidden']").val();
        $("#inex-content").load('files/'+ pck + '/inclusion_exclusion.html',null,function(){
			inexstr=$("#inex-content").html().replace(/&lt;/g,"<");
			inexstr=inexstr.replace(/&gt;/g,">");
            jQuery.facebox(inexstr);
        });
        
    });
    
    $("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()) ;
    				$("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 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( mapping[0][city + ' ' +cat ] ) ;
 // alert( mapping[0]["Chennai Standard"] );
  price_id = null;
  
  if( city != null && cat != null ){
      price_id = mapping[ pck ][city + ' ' +cat ];
  }
  else if( city != null && cat == null ){
      price_id = mapping[ pck ][city ];
  }
  else if( city == null && cat != null ){
      price_id = mapping[ pck ][cat ];
  }
  else
  {
      price_id = mapping[ pck ];    
  }
  
  $("#pack-"+pck+" .pricediv").hide();
  $("#" + price_id).show();
  
  $("form[name='form-"+ pck +"'] input[type='hidden'][name='package_id']").val(price_id);
  
}

var user_index=(geo =='india')?0:1;
ddaccordion.init({
	headerclass: "submenuheader", //Shared CSS class name of headers group
	contentclass: "submenu", //Shared CSS class name of contents group
	revealtype: "click", //Reveal content when user clicks or onmouseover the header? Valid value: "click" or "mouseover
	mouseoverdelay: 200, //if revealtype="mouseover", set delay in milliseconds before header expands onMouseover
	collapseprev: true, //Collapse previous content (so only one open at any time)? true/false
	defaultexpanded: [user_index], //index of content(s) open by default [index1, index2, etc] [] denotes no content
	onemustopen: false, //Specify whether at least one header should be open always (so never all headers closed)
	animatedefault: true, //Should contents open by default be animated into view?
	persiststate: false, //persist state of opened contents within browser session?
	toggleclass: ["", ""], //Two CSS classes to be applied to the header when it's collapsed and expanded, respectively ["class1", "class2"]
	togglehtml: ["suffix", "<img src='webroot/images/plus.gif' class='statusicon' />", "<img src='webroot/images/minus.gif' class='statusicon' />"], //Additional HTML added to the header when it's collapsed and expanded, respectively  ["position", "html1", "html2"] (see docs)
	animatespeed: "fast", //speed of animation: integer in milliseconds (ie: 200), or keywords "fast", "normal", or "slow"
	oninit:function(headers, expandedindices){ //custom code to run when headers have initalized
		//do nothing
	},
	onopenclose:function(header, index, state, isuseractivated){ //custom code to run whenever a header is opened or closed
		//do nothing
	}
   });
   
   
   function NewWindow(mypage,myname,w,h,scroll){
    /*LeftPosition = (screen.width) ? (screen.width-w)/2 : 0;
    TopPosition = (screen.height) ? (screen.height-h)/2 : 0;
    settings = 'height='+h+',width='+w+',top='+TopPosition+',left='+LeftPosition+',scrollbars='+scroll+',resizable'
    win = window.open(mypage,myname,settings)*/
  	window.open(mypage,myname,'scrollbars=yes,width=725,height=600');
}

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;
}