/*
* Global JavaScript (requires jQuery v1.3.2)
*
* Original Author: Chris Martin, Webexpectations.com
* Last updated: 2009/09/14
*
* Other Authors:
* Karen Comber, Webexpectations.com
* Aliya Hirji, Webexpectations.com
*
*/

/* - Initialisation (runs functions common to all pages) - */

$(document).ready(function() {
    enhanceButtons();
    buttonHover();
    attachNavbarHovers();
    attachNavbarHeightAdjust();
    durationSelect();
    attachDatePickers();
    stateShow();
    slideShow();
    printButton();
    divSlider();
    checkSifr();
    popUp();
    clearFieldsOnClick();
    try { initializeGoogleMaps(); } catch (e) { }
    removeCrossPagePosts();

    //Safari style adjustments
    if ($.browser.safari) {
        /*$('div.products div.back-top-right').css('padding-bottom','91px');*/
        $('div.products-side div.back-border').css('padding-bottom', '23px');
        $('div.checkout1 div.back-border').css('min-height', '409px');
        $('div.checkout1 div.card').css('min-height', '411px');
        $('div.checkout1 div.your-booking').css('min-height', '411px');
        $('div.column201 div.your-booking').css('padding-bottom', '10px');
        $('div.column706 div.back-border div.back-top-right-receipt').css('padding-bottom', '15px');
        $('div.main-checkout div.column340-top').css('margin-bottom', '12px');
        $('div.column706 div.back-border div.back-top-right-receipt').css('padding-bottom', '13px');
        //$('div.homesignposts div.inthenews div.sw1').css('min-height','212px');
        //$('div.homesignposts div.findalocation div.sw1').css('min-height','131px');
        //$('div.homesignposts div.quickquote div.r-icon div.icon').css('margin-top','-3px');
        //$('div.homesignposts div.quickquote h2').css('margin-bottom','15px');
        $('div.yourmr div.homebutton').css('padding-top', '6px');
        $('div.yourbw div.homebutton').css('padding-top', '17px');
        $('div.generic div.back-top div.products-back').css('min-height', '296px');
        $('div.column201 div.happensnext').css('min-height', '183px');
        $('div.column-flex div.r-empty').css('min-height', '41px');
        $('div.pop-up div.image-content').css('height', '150px');
        $('div.pop-up div.map-content').css('height', '150px');
    }
    else {
        $('select').addClass('notSafari');
    }

    $.browser.msie6 = $.browser.msie && ($.browser.version &&
$.browser.version < 7 || /6.0/.test(navigator.userAgent));

    if ($.browser.msie6) {
        addImgAttr();
    }


});

/*$(window).load(function() {
productsImage();
});*/

function showLoading() {
    $.fn.colorbox({ inline: true, href: "#Loading", open: true, opacity: 0.3, overlayClose: false });
}


function addImgAttr() {

    $('img.icon').each(function() {

        var imgWidth = $(this).width();
        var imgHeight = $(this).height();

        if (imgWidth != 'undefined' && imgHeight != 'undefined') {
            $(this).attr('width', imgWidth);
            $(this).attr('height', imgHeight);

            if ($.browser.msie && $.browser.version == "6.0") {
                $(this).css('behavior', 'url(/_htc/png.htc)');
            }
        }
    });
}


function enhanceButtons() {
    $('input.button').each(function() {
        var button = $(this);
        $(this).addClass('hide');

        var a = $("<a></a>").click(function() {
            button.click();
            //$('form').submit();
        });

        a.addClass('button');
        var span = $('<span></span>');
        a.append(span);
        a.append($(this).val());
        $(this).after(a);
    });

    $('input.searchbutton').each(function() {
        var button = $(this);
        $(this).addClass('hide');

        var a = $("<a></a>").click(function() {
            button.click();
            //$('form').submit();
        });

        a.addClass('button');
        var span = $('<span></span>');
        a.append(span);
        var em = $('<em></em>');
        em.append($(this).val());
        a.append(em);
        $(this).after(a);
    });

    $('input.postbutton').each(function() {
        var button = $(this);
        $(this).addClass('hide');

        var a = $("<a></a>").click(function() {
            button.click();
        });
        a.addClass('button');

        var span = $('<span></span>');
        a.append(span);
        a.append($(this).val());

        $(this).after(a);
    });
}

function buttonHover() {
    $("a.button").mouseover(function() {
        $(this).addClass("over");
    });
    $("a.button").mouseout(function() {
        $(this).removeClass("over");
    });
}

function attachNavbarHovers() {
    $('ul.navbar li').each(function() {
        $(this).hover(
      function() {
          $(this).addClass('hover');
      },
      function() {
          $(this).removeClass('hover');
      }
    );
    });

    $('ul.navbar li:first').each(function() {
        $(this).hover(
      function() {
          $(this).addClass('firsthover');
      },
      function() {
          $(this).removeClass('firsthover');
      }
    );
    });
}

function attachNavbarHeightAdjust() {
    $('ul.navbar li div.subnav ul li').each(function() {
        $(this).hover(
      function() {
          var theheight = $(this).closest('div.sw1').height().toString();
          theheight = parseInt(theheight.replace('px', ''));
          newheight = theheight - 5;
          $(this).find('div.subsubnav div.ssw2').css('height', newheight + 'px');
      },
      function() {

      }
    );
    });
}

function durationSelect() {
    $("div.sideform input.hourly").click(function() {
        $("div.sideform div.halfday").hide();
        $("div.sideform div.hourly").show();
    });
    $("div.sideform input.halfday").click(function() {
        $("div.sideform div.hourly").hide();
        $("div.sideform div.halfday").show();
    });
    $("div.sideform input.fullday").click(function() {
        $("div.sideform div.hourly").hide();
        $("div.sideform div.halfday").hide();
    });
}

function attachDatePickers() {
    var thepicker = new Array();
    $('input.datepicker').each(function(i) {
        if ($(this).hasClass('meetingrooms')) {
            $(this).datepicker({
                dateFormat: 'dd/mm/yy',
                minDate: new Date(),
                beforeShowDay: $.datepicker.noWeekends
            });
        } else {
            $(this).datepicker({
                dateFormat: 'dd/mm/yy',
                minDate: new Date()
            });
        }
        var pickericon = $("<div></div>");
        pickericon.addClass('icon');
        pickericon.addClass('calendar');
        thepicker[i] = $(this);
        pickericon.click(function() {
            thepicker[i].focus();
        });
        $(this).before(pickericon);
    });
}

/* Show hide state for US  */
function stateShow() {
    $('select.stateshow').each(function() {

        if ($(this).val() == 'US') $(this).parent().next().removeClass('hide');
        else $(this).parent().next().addClass('hide');

        $(this).change(function() {
            if ($(this).val() == 'US') $(this).parent().next().removeClass('hide');
            else $(this).parent().next().addClass('hide');
        });
    });
}

function printButton() {
    $("a.print").each(function() {
        $(this).click(function() {
            print();
        });
    });
}

/*
function productsImage() {
var image = $("img.back-products");
image.removeAttr('width');
image.removeAttr('height');
image.attr('width', image.width());
image.attr('height', image.height());
image.addClass('png');
image.css('display','block');
$("div.products div.back-top-right").css('position','relative');
}
*/

function slideShow() {

    var slides = $('.slide');
    var numberOfSlides = slides.length;
    var currentSlide = $('.slide:first');
    var slideCounter = 1;
    $('div.signpost-inside-dark-top').show();
    $('div.signpost-inside-dark-bottom').show();
    if (numberOfSlides == 0) {
        showMap();
        $('img.businesscenter-map').show();
        $('img.businesscenter-map-lg').show();
        $('.centerdetails').hide();
        $('div.pop-up .centerdetails').hide();
        $('div.signpost-inside-dark-top').hide();
        $('div.signpost-inside-dark-bottom').hide();
    }
    if (numberOfSlides > 1) {
        $('.centerdetails').show();
    } else if (numberOfSlides == 1) {
        if ($('.centerdetails').hasClass('.nomaplink')) {
            $('div.pop-up .centerdetails').hide();
        } else {
            $('.centerdetails').show();
            $('div.centerdetails a.map').show();
            $('span.control').hide();
            $('div.centerdetails a.images').hide();
            $('div.centerdetails span.divider').hide();
        }
    }

    $('a.map').click(function() {
        $('div.pop-up div.column201-trans').hide();
        $('.centerdetails').show();
        $('span.control').hide();
        $('div.image-content').hide();
        $('.slideshow').hide();
        $('img.businesscenter-map').show();
        showMap();
        $('div.map-content').show();
        $('div.centerdetails a.images').show();
        if (numberOfSlides > 1) {
            $('div.centerdetails a.images').text('Images');
        } else {
            $('div.centerdetails a.images').text('Image');
        }
        $('div.centerdetails a.map').hide();
        $('div.centerdetails span.divider').hide();
    });

    $('a.images').click(function() {
        $('div.pop-up div.column201-trans').show();
        $('div.map-content').hide();
        $('img.businesscenter-map').hide();
        hideMap();
        $('.slideshow').show();
        $('div.image-content').show();
        if (numberOfSlides > 1) {
            $('.centerdetails').show();
            $('span.control').show();
            $('div.centerdetails a.images').show();
            $('div.centerdetails span.divider').show();
            $('div.centerdetails a.images').text('More Images');
            if ($('.centerdetails').hasClass('.maplink')) $('div.centerdetails a.map').show();
        } else {
            if ($('.centerdetails').hasClass('.nomaplink')) {
                $('div.pop-up .centerdetails').hide();
            } else {
                $('.centerdetails').show();
                $('div.centerdetails a.map').show();
                $('span.control').hide();
                $('div.centerdetails a.images').hide();
                $('div.centerdetails span.divider').hide();
            }
        }
    });

    $('.rightControl').click(function() {
        if (slideCounter < numberOfSlides) {
            currentSlide.hide();
            currentSlide = currentSlide.next().show();
            slideCounter++;
        } else {
            currentSlide.hide();
            currentSlide = $('.slide:first').show();
            slideCounter = 1;
        }
    });

    $('.leftControl').click(function() {
        if (slideCounter > 1) {
            currentSlide.hide();
            currentSlide = currentSlide.prev().show();
            slideCounter--;
        } else {
            currentSlide.hide();
            currentSlide = $('.slide:last').show();
            slideCounter = numberOfSlides;
        }
    });

}

function divSlider() {
    var sliders = $("div.slider");
    $("div.products h5").addClass('contract');
    sliders.each(function() {
        $(this).hide();
        $(this).prev().children('a.slidelink').click(function() {
            $slider = $(this).parent().next();
            if ($slider.is(":hidden")) {
                $slider.slideDown();
                $slider.prev().removeClass('contract');
                $slider.prev().addClass('expand');
            } else {
                $slider.slideUp();
                $slider.prev().removeClass('expand');
                $slider.prev().addClass('contract');
            }
        });
    });
}

function popUp() {

    try {
        var profiles =
    {

        businesscenter:
    	{
    	    height: 501,
    	    width: 442,
    	    center: 1,
    	    scrollbars: 0,
    	    resizable: 0,
    	    status: 0
    	},
        quickquote:
			{
			    height: 280,
			    width: 220,
			    center: 0,
			    scrollbars: 0,
			    resizable: 0,
			    status: 0
			}

    };

        $(function() {
            $(".popupwindow").popupwindow(profiles);
        });

        if ($.browser.safari) {
            if ($('body div:first').hasClass('businesscenter')) window.resizeTo(459, 532);
        }
    }
    catch (e) { }

}


//switches on sifr unless it is a country that does not support language set - language
//identifier present as class in body tag
function checkSifr() {
    var arrNoSifr = ["cn", "kr", "zh"];
    var arrLang = $("body").attr("class").split(" ");
    if (jQuery.inArray(arrLang[0], arrNoSifr) == -1) $("body").addClass("sifr");
}

function showMap() {
    var mapHeight = $('div.map_wrapper div#map_canvas').css('height');
    $('div.map_wrapper').css('height', mapHeight);
    $('div.map_wrapper').css('visibility', 'visible');
}
function hideMap() {
    $('div.map_wrapper').css('height', '0px');
    $('div.map_wrapper').css('visibility', 'hidden');
}

function clearFieldsOnClick() {
    $('.text').each(function() {
        var default_value = this.value;
        $(this).focus(function() {
            if (this.value == default_value) {
                this.value = '';
            }
        });
        $(this).blur(function() {
            if (this.value == '') {
                this.value = default_value;
            }
        });
    });
}

function removeCrossPagePosts() {
    var $form = $('form.MainSite'); //Only effects main site, NOT the Checkout site
    var sAction = $form.attr('action');
    sAction = /\?.+/.exec(sAction);

    if (sAction != null && sAction != '') {
        $form.attr('action', sAction);
    }
};