/* 
crann.js: library of javascript for animation / branding effects
    david.kelly@fanore.com (http://www.fanore.com/website_design.htm)
    
    
    
*/

function setup_page(){
    if ($('#image_payload').length){
    var buffer=$('#image_payload').val();
    var url=$('#u_base').val() + 'images/'
    gallery=new make_gallery(url,buffer, 'gallery_image');
    }

    if ($('#map').length){
        eval('load_map();');
    }
    

    $('#search_form').submit(
        function(){
        return eval('validate_search();')
        });
        
    if ($('#form_contact_us').length)
    {
        setup_focus();
        $('#form_contact_us').submit(
            function(){
            return eval('validate_booking();')
            });
    }
    
	
    
    if ($('#map').length){
        eval('load_map();');
    }
    fix_height();
}



function fix_height(){
 
    var h=$(window).height()-176;
    //alert(h);
    //alert($('#main_panel').height())
    if (($('#main_panel').height()) < h){
       $('#main_panel').height(h);
    }
    else{
       $('#main_panel').height('auto');    
    }
    //alert($('#main_panel').height());
}

function setup_focus(){
    
}
//submit_form: utility that will perform client side validation of the passed form id and allow validator to post it 
function submit_form(param_form){
    var el=get_el("cs_validation");
    //alert("here");
    if (el){
        var f=$(param_form)
        var cs_validation=f['cs_validation'];
         return eval($(cs_validation).getValue());
    }
    else{
        $(param_form).submit();
    }
}

function open_link(url){
 var settings='Height=' + screen.height + ',Width=' + screen.width + ',Top=0,left=0,scrollbars=yes,resizable=1, location=1,status=1'
 var w=window.open(url,'ggl',settings);    
}

function validate_search(){
    if ($('#txt_keywords').val().length==0){
        alert("please enter one or more keywords to perform a search!");
        $('#txt_keywords').focus();
        return false;
    }
   // $('search_details').value="";
   // $('search_form').submit();
   return true;
}


function validate_booking(){
//    alert('here');
    //contact name
    if ($('#txt_name').val().length==0){
        alert("Please enter your name!");
        $('#txt_name').focus();
        return false;
    }
    
    //email address
    if ($('#txt_email').val().length==0){
        alert("Please enter your email address!");
        $('#txt_email').focus();
        return false;
    }
    var e=$('#txt_email').val();
    if (!check_email(e,true)){
        alert("Please check that you have entered a valid  email address!");
        $('#txt_email').focus();
        return false;
    
    }
    //telephone
    var t=fix_telephone($('#txt_telephone').val());
    $('#txt_telephone').val(t)
    if (($('#txt_telephone').val().length==0)||($('#txt_telephone').val().length < 6)){
        alert("Please enter a contact telephone number!");
        $('#txt_telephone').focus();
        return false;
    }
    var tmp=validate_dmy('arrival');
    if (tmp==false){
        return false
    }
    $('#arrival_date').val(tmp);
    var tmp=validate_dmy('departure');
    if (tmp==false){
        return false
    }
    $('#departure_date').val(tmp);
    
    
    $('#your_details').val('');
    //
    
    return true;
 }
 

//validate_dmy: return false if the day month year  are invalid
function validate_dmy(param_prefix){
    var rule_day=0;
    var rule_month=0;
    var rule_year=0;
    
    //alert(param_prefix);
    
    rule_day=$('#' + param_prefix + "_day").val();
    rule_month = $('#' + param_prefix + "_month").val();
    rule_year = $('#' + param_prefix + "_year").val();
    var s_date=rule_day + " " + rule_month +  " " + rule_year  
    var d=new Date(s_date);
    //we test the date is valid by checking the date value against the selectedIndex value (should always be 1 less than selectedIndex)
    if (d.getDate()!=(parseInt(rule_day))){
        show_error_message("You have selected an invalid day of the month, please check your entry!",'error_message_panel', false);
        $('#' + param_prefix + "_day").focus();
        return false;
    }
    
    return s_date;
}


function check_email(svalue, bmail){
    var str=svalue;
    var sinvalid="!,£,\$,%,',\&";
    
    if (bmail==true){
    	var reg1 = /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)|(')|(%)|(")/; // not valid
    	var reg2 = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/; // valid
    }
    else{
    	var reg1 = /(\.\.)|(^\.)|(')|(%)|(")/; // not valid
    	var reg2 = /([a-zA-Z0-9])|([a-zA-Z])|([0-9])/; // valid
    
    }
    if (!reg1.test(str) && reg2.test(str)) { // if syntax is valid
    	return true;
    	}
    return false;
}



//fix_telephone: removes non-numeric characters from user entry
function fix_telephone(param_value){
    var ret_value='';
    var wip_value=param_value;
    var a_values='1234567890+()'
    //alert(wip_value.length);
    for (var i=0;i<=wip_value.length;i++){
    //alert(wip_value.charAt(i))
        for (var x=0;x<=a_values.length;x++){
            if (wip_value.charAt(i)==a_values.charAt(x)){
                ret_value+=wip_value.charAt(i)
            }
        }
    } 
    //alert(ret_value);
    return ret_value;   
}

//check_format: of a file
function check_format(param_value, param_type){

            
 if (param_value.length!=0){
        //check for allowed file types
        var s_ext=param_value.substr(param_value.length-3,3).toLowerCase();
        switch(s_ext){
            case "jpg": //ok
                return true;
                break;
            case "gif":
                return true;
                break;
            case "tif":
                return true;
                break;
            case "pdf":
                return true;
                break;
            case "mdi":
                return true;
                break;
            case "doc":
                return true;
                break;
            case "ocx":
                return true;
                break;
            case "png":
                return true;
                break;
        }
}

return false;
}



function load_map() {
//  alert("map")
    var lat=53.12556885381429;
    var lon=-9.276902675628662;
    var c_lat=53.212612189941574;
    var c_lon=-9.16259765625;
    if (GBrowserIsCompatible()) {
        var map = new GMap(document.getElementById("map"));
        map.addControl(new GLargeMapControl())
        map.addControl(new GMapTypeControl());
        map.setCenter(new GLatLng(c_lat, c_lon), 7);
        //var blueIcon = new GIcon(G_DEFAULT_ICON);
        //blueIcon.image = "http://www.google.com/intl/en_us/mapfiles/ms/micons/blue-dot.png";      
        //Fanore Holiday Cottages, Fanore, Co. Clare, Ireland. Tel: +353 65 707 6164 Email: info@fanorecottages.com   		
        var html='<strong>Fanore Holiday Cottages.</strong><br/>Fanore<br/>Co. Clare<br/>Ireland<br/>Telephone: +353 65 707 6164<br/>Email: info@fanorecottages.com'
        var point = new GPoint(parseFloat(lon), parseFloat(lat));
        var marker = new GMarker(point);
        var icon = new GIcon(G_DEFAULT_ICON);
        icon.image = "http://www.google.com/intl/en_us/mapfiles/ms/micons/blue-dot.png";
        icon.iconSize = new GSize(20,20);
        icon.iconAnchor = new GPoint(10,20);
        icon.infoWindowAnchor = new GPoint(10, 20);
        var marker = new GMarker(point,icon);   
        GEvent.addListener(marker, "mouseover", function() {
        marker.openInfoWindowHtml(html);
        });
        map.addOverlay(marker);
        return marker
    }
 }
 
 //make_gallery: construct javascript object for animation of gallery
function make_gallery(param_url,param_images, param_target){
    this.url=param_url
    this.contents=new Array;
    this.current=0;
    this.target=param_target
    this.next=next_gallery_image
    this.start=start_gallery_animation
    this.update=show_current_image
    var b=param_images.split(',');
    for (var i=0;i < b.length;i++){
        var img=new Image 
        img.src=this.url + b[i]; 
        this.contents[this.contents.length]=img.src 
        if (i==1){
            this.start();
        }
    }
    return this;
}

function next_gallery_image(){

    var i=this.contents.length;
    this.current++
    if (this.current==i){
        this.current=0;
    }
    return this.contents[this.current]
}

function start_gallery_animation(){
					$('ul#image_gallery').innerfade({
						speed: 2000,
						timeout: 4000,
						type: 'sequence',
						containerheight: '266px'
					});
					$('li','#image_gallery').removeClass('no_show');
    
  //  setTimeout(function(){
  //      gallery.update();
  //  },3000)
}

function show_current_image(){
    var src=this.next();
    //alert(this.target);
    $('#' + this.target).attr('src',src);
    setTimeout(function(){
        gallery.update();
    },3000)
}

function show_error_message(msg,param_el,param_bool){
alert(msg);
}