
// DOCUMENT READY 
$(document).ready(function (){

    // initialise hint.js
    $('input:text').hint();

    // apply cufon
    applyCufon();  

    hidePackageDetails();
    
    // image swapper
    imageSwapper();
    tmtImageSwapper();
    
    // navigation functions
    initialiseNav();
    navImageRollovers();

    preloadImages();

    $('#dialog').jqm();
    $('#brdialog').jqm({trigger:'.dynReqBroc'})

    // ieNavFix();

    hideShowSlider(".conferenceHome .introParagraph",127);
    hideShowSlider("#conferenceIntro .introParagraph",100);
    hideShowSlider("#popularDestinations",194);
    
    // initialize scrollable  
    $(".conferenceHome div.scrollable").scrollable({
        size: 5,
        clickable: false,
        next: "#scrollNext",
        prev: "#scrollPrev",
        navi: "#navi"
    });
    
    $(".tmtHome div.scrollable").scrollable({
        size: 8,
        clickable: false,
        next: "#scrollNext",
        prev: "#scrollPrev",
        navi: "#navi"
    });
    
     $("#conferencePanel div.scrollable").scrollable({
        size: 2,
        interval: 6000,
        loop: true,
        speed: 600,
        clickable: false,
        next: "#scrollNext",
        prev: "#scrollPrev",
        navi: "#navi"
    });
    
    // search clicks
    $(".search .searchBtn").bind("click", function(event){
        search();
        event.preventDefault();
    });
    
    $(".search .search_input").bind("keypress", function(event){
        if (event.keyCode == 13) { search(); }
    });
    
    $("input.tmtMoreInfoBtn").bind("click", function(e){
        courseID = $('#notSureWhere select').val();
        if (courseID == 258 || courseID == 281 || courseID == 137 || courseID == 5 || courseID == 286)
        {
        	window.location.href = "/pages/tailormade/Locations.aspx?lid=" + courseID;
        }
        else
        {
        	window.location.href = "/pages/tailormade/Countries.aspx?cids=" + courseID;
        }
    });
    
    // SEARCH FUNCTION -------------------------------------------------------------------------------------------------
    function search() {
        searchTerm = $('.search .search_input').attr('value');
        window.location.href = "/pages/home/search.aspx?s=" + searchTerm;
    }    
    
    tmtSliders("accExamples");
    tmtSliders("subjectVisitIdeas");
    tmtSliders("generalVisitIdeas");
    tmtSliders("accommodationExamples")
});

function preloadImages() {
    if (document.images) {
        navPopupTop = new Image(); 
        navPopupTop.src = "/images/navigation/navBoxBtm.png"; 
    
        navPopupBtm = new Image(); 
        navPopupBtm.src = "/images/navigation/navBoxTop.png"; 
    }

}

//  applyCufon() : text replacement for the listed elements
function applyCufon() {
    Cufon.replace
    ('a.orangeBtn span', { fontFamily: 'Avant Guard' })
    ('#header p', { fontFamily: 'Avant Guard' })
    ('span#topNavItems a', { fontFamily: 'Avant Guard' })
    ('h2', { fontFamily: 'Avant Guard' })
    ('#conferenceIntro p.conferencePrice', { fontFamily: 'Avant Guard' })
    ('span.price', { fontFamily: 'Avant Guard' })
    ('h1', { fontFamily: 'Avant Guard' })
    ('#dialog #progressBar p', { fontFamily: 'Avant Guard' })
    ('#conferencesScroller span.overlay', { fontFamily: 'Techno' })
    ('#conferencesScroller a.price', { fontFamily: 'Avant Guard' })
    ('#countryListPage a.viewThisLink', { fontFamily: 'Avant Guard' })
    ('#membersAreaLink > a', { fontFamily: 'Avant Guard' })
    ('#conferencePanel span.date ', { fontFamily: 'Avant Guard' })
    ('#conferencesScroller span.image span.imageOverlay', { fontFamily: 'Avant Guard' })
    ('#tailormadePanel .header', { fontFamily: 'Avant Guard' })
    ('#offersRotator span.countryBlock', { fontFamily: 'Avant Guard' })
    ('#offersRotator span.priceBlock', { fontFamily: 'Avant Guard' })
    //('#packageTravelOptions .package h3', { fontFamily: 'Avant Guard Demi' })
    ;
}

//  reapplyCufon() : text replaces the ajax elements in the nav
function reapplyNavCufon() {
    // alert("applying cufon");
    Cufon.replace
    ('#dialog h2', { fontFamily: 'Avant Guard' })
    ('#dialog .popup_images a .popup_button span', { fontFamily: 'Avant Guard' });
}

//  initialiseNav() : initialises the hover states on the nav buttons
function initialiseNav() {
    // add selected state and show the first sub menu
    $('ul.browseBySubject');
    $('#topNavItems #browseBySubject').addClass('selected');
    
    // bind a hover event to each top nav button
    $('#topNavItems a.navHover').bind("mouseenter", function(e) { 
        navigationChange($(this).attr("id")); 
        e.preventDefault();
    });
    
    $('#topNavItems a.navHover').bind("click", function(e) {  
        e.preventDefault();
    });
}

//  navigationChange([element being hovered passed from initialiseNav()]) : changes displayed
//  sub menu and hover state on button
function navigationChange(elementID) {
    // alert(elementID);
    
    //$("ul.subnav li").hide();
    
    $('#topNavItems a.navHover').removeClass("selected");
    $("#" + elementID).addClass("selected");
    
    $('.subnav').css("display","none");
    $('.' + elementID).css("display","block");
    
    operaRefresh();
}

// ieNavFix() : fixes the navigation max widths in IE6 - no longer used
function ieNavFix() {
    $('ul.subnav li a').each( function() {
        if ($(this).width() > 93) { x = "93px" } else { x = "auto" }
        $(this).css("width",x);
    });
}

// navImageRollovers() : rollover states for the navigation images
function navImageRollovers() {
    // on hover add a corner id
    $('div.popup_images a').bind("mouseenter", function(e) { 
        currentSrc = $(this).find("img").attr("src");
        $(this).find("img").attr("src", currentSrc + "&crn=1@0|0");
    });
    
    // on hover off remove corner id
    $('div.popup_images a').bind("mouseleave", function(e) { 
        currentSrc = $(this).find("img").attr("src");
        currentSrc = currentSrc.replace('&crn=1@0|0','');
        $(this).find("img").attr("src", currentSrc);
    });
}

function hideShowSlider(element,cutOff) {
    if ( $(element).length > 0 ) { 
        // alert(element + " exists"); 
        // get full height of element
        elementHeight = $(element).outerHeight();
        // apply a height property to the element, therefore cutting it off
        
        // alert(elementHeight);
        
        if (elementHeight > cutOff) {
            $(element).css("height",cutOff + "px");  
            // get the elements class
            elementClass = $(element).attr("class");
            // add a show more link after the element with a class of [elementClassMore]
            $(element).after("<a class='readMore " + elementClass + "More' href='#more' title='Read more'><span>Show all</span></a>");
            
            // bind a click event to the created link
            $("a." + elementClass + "More").bind("click", function(e) {
                // alert(elementHeight);
                // alert($(element).css("height"));
                
                // if its current height is the hidden height, animate to full height, change text to hide and change img bg pos
                if($(element).css("height") == cutOff + "px") {
                    // alert("animating down");
                    // alert(cutOff);                
                    
                    $(element).css({height:cutOff }).animate({ height: elementHeight}, 750);
                    $("a." + elementClass + "More span").text("Hide");
                    $("a." + elementClass + "More span").css("background-position","right -21px");
                }            
                // otherwise do the opposite
                else {
                    // alert("animating up");
                    // alert(elementHeight);
                    $(element).css({height: elementHeight }).animate({ height: cutOff }, 750); 
                    $("a." + elementClass + "More span").text("Show all");
                    $("a." + elementClass + "More span").css("background-position","right 5px");
                }
                e.preventDefault();
            });
        }
    }
}

function imageSwapper() {
    $("#imageSwapper img.subImage1, #imageSwapper img.subImage2").bind("click", function(e) {
        clickedImgClass = $(this).attr("class");
        clickedImgSrc = $(this).attr("src");
        currentMainImgSrc = $("#imageSwapper img.mainImage").attr("src");
        
        $("#imageSwapper img.mainImage").attr("src",clickedImgSrc);
        $("#imageSwapper ." + clickedImgClass).attr("src",currentMainImgSrc);
    });
}

function tmtImageSwapper() {
    $("#imageSwapper img.sub1, #imageSwapper img.sub2, #imageSwapper img.sub3").bind("click", function(e) {
        // alert("click");
        clickedImgClass = $(this).attr("class");
        clickedImgSrc = $(this).attr("src");
        currentMainImgSrc = $("#imageSwapper img.mainImg").attr("src");
        
        $("#imageSwapper img.mainImg").attr("src",clickedImgSrc);
        $("#imageSwapper ." + clickedImgClass).attr("src",currentMainImgSrc);
    });
}

function SetTooltip(sID) {
    // alert("hello");

    var sURL = '/pages/conferences/speakerdetail.aspx?id=' + sID;
    var sDiv = "#Speaker" + sID;
    
    // alert(sURL);
    // alert(sDiv);
    
    $(sDiv).jHelperTip({
	    trigger: "hover",
	    source: "ajax",
	    type: "GET", 
	    url: sURL,
	    data: '',
	    loadingImg: '/images/shd/ajax-loader.gif',
	    loadingText: 'loading...',
	    autoClose: false, 
	    topOff: 15,
	    leftOff: 10,
	    callback: function(){ alert("red"); speakerCufon(); } 
    });
}

function hidePackageDetails() {
    $packageCounter = 0;
    $('#packageTravelOptions .package .innerPackage').each( function() {
        if($packageCounter == 0) {
            $(this).parent().append("<a class='open readMore' rel='"+ $(this).parent().attr('id') +"' href='#more' title='Read more'><span>Hide all</span></a>");
            $(this).parent().find(" a.readMore span").css("background-position", "right -21px");
            $packageCounter++;
        } else {
            $(this).hide();
            $(this).parent().append("<a class='readMore' rel='"+ $(this).parent().attr('id') +"' href='#more' title='Read more'><span>Show all</span></a>");
        }
    });
    
    $("#packageTravelOptions .package a.readMore").bind("click", function(){
        elementToSlide = $(this).attr("rel");
        $("#packageTravelOptions #"+ elementToSlide +" .innerPackage").slideToggle(600);
    
        if($('#packageTravelOptions #'+ elementToSlide +' a').is('.open')) {
            $(this).removeClass("open");
            $(this).parent().find("span").text("Show all");
            $(this).parent().find("span").css("background-position","right 5px");
        } else {
            $(this).addClass("open");
            $(this).parent().find("span").text("Hide all");
            $(this).parent().find("span").css("background-position","right -21px");
        }
    
    });   
}

function operaRefresh() {
    if (window.opera) { 
    	//alert($("body").css("position"));
    	if ($("body").css("position") === 'static') {
    		$("body").css("position","relative");
    	} else {
    		$("body").css("position","static");
    	}
    }
}

function tmtSliders(divName) {
    divName = "#" + divName;
    
    if ( $(divName).length > 0 ) {
        
        if ($(divName + "  ul li").length > 1) {
           
            // add slider class to the ul
            $(divName + " ul").addClass("slider");
            
            // find the first child add a class to it
            $(divName + " ul li:first-child").addClass("firstItem");
            
            // seperate the first child into a seperate ul
            $(divName).find('.firstItem').insertBefore(divName +' ul').wrapAll("<ul></ul>");

            // hide the slider ul, and append the a link
            $(divName + " ul.slider").hide();
            $(divName).append("<a href='#' title='View more' class='readMore'><span>View more</span></a>");
            
            $(divName + " a.readMore").bind("click", function(e) {
                $(divName + " ul.slider").slideToggle(1000);
                
                if($(divName + " ul.slider").is(".open")) {
                    $(divName + " ul.slider").removeClass("open");
                    $(this).find("span").text("View more");
                    $(this).find("span").css("background-position","right 5px");            
                } else {
                    $(divName + " ul.slider").addClass("open");
                    $(this).find("span").text("Hide");
                    $(this).find("span").css("background-position","right -21px");
                }
                e.preventDefault();
            });
        }
    }
}
    