// JavaScript Document
/* Added by Endava */
// Javascript Variable for storind default text for Find a rule textbox
var FindARuleDefaultText = "";

jQuery(document).ready(function() {
    bindEvents();
});

function bindEvents() {

    function tooltip() {
        jQuery(".nav_main a.main[title]").tooltip({
            // use div.tooltip as our tooltip
            tip: '.nav_tooltip',
            // use fade effect instead of the default
            effect: 'fade',
            // make fadeOutSpeed similar to browser's default
            fadeOutSpeed: 100,
            // the time before tooltip is shown
            predelay: 200,
            // tweak the position
            position: "top center",
            offset: [-4, 0]
        });
    }

    //initialise tooltips
    tooltip();

    // set up mega dropdrown
    function addMega() {
        jQuery(this).addClass("hovering");
        var btn = jQuery(this).find("a.main");
        btn.addClass("megaactive");
    }
    function removeMega() {
        jQuery(this).removeClass("hovering");
        var btn = jQuery(this).find("a.main");
        btn.removeClass("megaactive");
    }

    var megaConfig = {
        interval: 400,
        sensitivity: 4,
        over: addMega,
        timeout: 400,
        out: removeMega
    };
    jQuery("li.mega").hoverIntent(megaConfig);


    // mpu
    jQuery("#mpu").tabs({ fx: { opacity: "toggle", duration: "slow"} }).tabs("rotate", 8000, true);
    jQuery(".ui-tabs-panel").hover(
				function() {
				    jQuery("#mpu").tabs("rotate", 0, true);
				},
				function() {
				    jQuery("#mpu").tabs("rotate", 8000, true);
				}
			);

	
	//start: Chanmpionship widget
    //create championships scrollable
    var championshipScrollApi = jQuery("div.championship_items").scrollable({
        size: 1,
        clickable: false,
        api: true
    });
    var currentChampionship = 0;
    var championshipCount = 0;
	var pageSeparatorOf = '';
	
	if (jQuery(".championship_items div.items").length > 0) {
 		championshipCount = $(".championship_items div.items")[0].children.length;
		pageSeparatorOf = $(".championships_panel .counter .pages")[0].innerHTML;
	if (championshipCount > 0) {
		currentChampionship = 1;
	}


        jQuery(".championships_panel a.nextPage").click(function() {
            if (currentChampionship < championshipCount) {
               currentChampionship++;
               //championshipScrollApi.next();
               setPageNumberText();
            }
        });

        jQuery(".championships_panel a.prevPage").click(function() {
            if (currentChampionship > 1) {
               currentChampionship--;
               //championshipScrollApi.prev(); 
               setPageNumberText();
            }
        });
    }
	
    function setPageNumberText() {
        jQuery(".championships_panel .counter .pages").text(currentChampionship + ' ' + pageSeparatorOf + ' ' + championshipCount);
    }

    setPageNumberText();

    //end: Chanmpionship widget
	
	
	var newsScrollApi = jQuery("div.latestnews_items").scrollable({
	size: 1,
	clickable: false,
	api: true
    });
	
    var currentNews = 0;
    var NewsCount = 0;
	var newspageSeparatorOf = '';
	
	if (jQuery(".latestnews_items div.items").length > 0) {
 		NewsCount = $(".latestnews_items div.items")[0].children.length;
		newspageSeparatorOf = $(".latestnews_panel .latestnews_counter .pages")[0].innerHTML;
	if (NewsCount > 0) {
		currentNews = 1;
	}


        jQuery(".latestnews_panel a.nextPage").click(function() {
            if (currentNews < NewsCount) {
               currentNews++;
               //newsScrollApi.next();
               setNewsPageNumberText();
            }
        });

        jQuery(".latestnews_panel a.prevPage").click(function() {
            if (currentNews > 1) {
               currentNews--;
               //newsScrollApi.prev(); 
               setNewsPageNumberText();
            }
        });
    }
	
    function setNewsPageNumberText() {
        jQuery(".latestnews_panel .latestnews_counter .pages").text(currentNews + ' ' + newspageSeparatorOf + ' ' + NewsCount);
    }

    setNewsPageNumberText();
	
	// end news scroll
	
	  jQuery("#shop_promo").scrollable({ hoverClass: 'hover', size: 1,clickable: false }).circular().navigator().autoscroll({
            steps: 1,
            interval: 6000,
            speed: "slow"	
        });

	//asign hover handlers to social network icons
	var socialtexts = [
					   "Facebook",
					   "Follow The R&A on Twitter",
					   "Stumble",
					   "Newsvine",
					   "RSS"
					  ]
	
	jQuery(".share_icons li a").each(function(i){
		i = i+1;
		i = i-1;	
		jQuery(this).hover(function(){
			jQuery("#share_text").text(jQuery("span",this).text());
		},
		function(){
			jQuery("#share_text").text("");
		}
		
		);

    });

    //assign handlers for homepage article tabs
    jQuery("#hp_playing_golf .tabs a").each(function(i) {
        var n = jQuery("#hp_playing_golf .tabs a").length;
        i = i + 1
        jQuery(this).click(function() {
            jQuery("#hp_playing_golf .tabs a").removeClass("active");
            jQuery(this).addClass("active");
            for (a = 1; a <= n; a++) {
                if (a == i) {
                    if (navigator.userAgent.indexOf("MSIE 6") == -1) {
                        jQuery(".tab_" + a).show();
                    } else {
                        jQuery(".tab_" + a).show("fast");
                    }

				}else{
					jQuery(".tab_"+a).hide();
				}
			}
			
		});
	});
	
	//assign handlers for homepage article tabs
	jQuery("#tabs_arrow_init .tabs_arrow a").each(function(i){
		var n = jQuery("#tabs_arrow_init .tabs_arrow a").length;
		i=i+1 
		jQuery(this).click(function(){
			jQuery("#tabs_arrow_init .tabs_arrow a").removeClass("active");
			jQuery(this).addClass("active");
		    for(a=1; a <= n; a++){
				if(a==i){
					if (navigator.userAgent.indexOf("MSIE 6")==-1){
						jQuery(".tab_"+a).show();	
					}else{
						jQuery(".tab_"+a).show("fast");
					}

				}else{
					jQuery(".tab_"+a).hide();
				}
			}
			
		});
	});
	
	jQuery("#tabs_arrow_init .letter_tabs a").each(function(i){
		var n = jQuery("#tabs_arrow_init .letter_tabs a").length;
		i=i+1
			if(jQuery(this).attr("class") != "disabled"){
				jQuery(this).click(function(){
				jQuery("#tabs_arrow_init .letter_tabs a").removeClass("active");
				jQuery(this).addClass("active");
				for(a=1; a <= n; a++){
					if(a==i){
						if (navigator.userAgent.indexOf("MSIE 6")==-1){
							jQuery(".tab_"+a).show();	
						}else{
							jQuery(".tab_"+a).show("fast");
						}
	
					}else{
						jQuery(".tab_"+a).hide();
					}
				}
			});
		}
		
	});
	
    // assign handlers for latest news module
    var curr_newslink = jQuery("#hp_latest_news .latestnews li a:first");

    jQuery("#hp_latest_news .latestnews li a").each(function(i) {
        var n = jQuery("#hp_latest_news .latestnews li a").length;
        i = i + 1
        jQuery(this).hover(function() {
            for (a = 1; a <= n; a++) {
                if (a == i) {
                    jQuery(this).addClass("active");
                    jQuery("#hp_latest_news .latestnews .img_" + a).show();
                    curr_newslink = jQuery(this);
                } else {
                    jQuery("#hp_latest_news .latestnews li a").removeClass("active");
                    jQuery("#hp_latest_news .latestnews .img_" + a).hide();
                }
            }
        }, function() {

        });
    });

    jQuery("#hp_latest_news .links_block").mouseleave(function() {
        curr_newslink.addClass("active");
    });
	
	// latest features
    var curr_featurelink = jQuery("#news_features .features li a:first");

    jQuery("#news_features .features li a").each(function(i) {
        var n = jQuery("#news_features .features li a").length;
        i = i + 1
        jQuery(this).hover(function() {
            for (a = 1; a <= n; a++) {
                if (a == i) {
                    jQuery(this).addClass("active");
                    jQuery("#news_features .features .img_" + a).show();
                    curr_featurelink = jQuery(this);
                } else {
                    jQuery("#news_features .features li a").removeClass("active");
                    jQuery("#news_features .features .img_" + a).hide();
                }
            }
        }, function() {

        });
    });

    jQuery("#news_features .links_block ul li a").mouseleave(function() {
        curr_featurelink.addClass("active");
    });
	
	// end latest features

    //ranking table functions
    jQuery("#hp_ranking_table table tr.datarow").hover(function() {
        jQuery(this).addClass("hovering");
    }, function() {
        jQuery(this).removeClass("hovering");
    });

    jQuery("#hp_ranking_table table tr.datarow").each(function(i) {
        i = i + 1;
        jQuery(this).click(function() {
            window.open(jQuery("#wagrPlayerLink").text() + jQuery("span", this).text());
        });
    });

    jQuery("#findarule").focus(function() {
        if (jQuery("#findarule").val() == FindARuleDefaultText) {
            jQuery("#findarule").val("");
        }
    });

    jQuery("#findarule").blur(function() {
        if (jQuery("#findarule").val() == "") {
            jQuery("#findarule").val(FindARuleDefaultText);
        }
    });

    // load the shop promo
   // loadShopPromo();

    //initialise the sub nav
    initMenu();

    jQuery(".sp_subnav").mouseleave(function() {
        t = setTimeout("closeSubNav()", 1500);
    });

    jQuery(".sp_subnav").mouseenter(function() {
        if (typeof (t) != "undefined") {
            clearTimeout(t);
        }
    });

    //initialise colorbox image viewer
	$("a[rel='photogroup_equipment']").colorbox();
	
	jQuery(".img_wrapper a[rel]").prepend('<div class="lbox"></div>');
	jQuery(".img_block_equipment_wrapper a[rel]").prepend('<div class="img_block_equipment_lbox"></div>');
	
	jQuery("a[rel='photogroup']").each(function(){
		var caption = jQuery(this).parent().parent().find('.img_caption').html();							   
		jQuery(this).colorbox({
		opacity:"0.85",
		previous:"",
		next:"",
		close:"Close",
		current:"{current} of {total}",
		title:caption
		});
	});

	jQuery("a[rel='photogroup_equipment']").each(function(){
		var caption = jQuery(this).parent().parent().find('.img_block_equipment_caption').html();							   
		jQuery(this).colorbox({
		opacity:"0.85",
		previous:"",
		next:"",
		close:"Close",
		current:"{current} of {total}",
		title:caption
		});
	});
    /* Added by Endava */	
	if (jQuery("#findarule").length > 0) {
        FindARuleDefaultText = jQuery("#findarule").val();
	}
	
// calendar
jQuery("a.calendarblock").click(function(){								 
	var container = jQuery(this).parent().parent();
	var mythis = jQuery(this);
	var content = mythis.next();
	var opened = container.find('.open');
	
	if (opened.length > 0) {
		if(content.hasClass('open')){
			content.removeClass('open').mycloseClasses().slideUp('100');
			return false;
		}
		opened.removeClass('open').mycloseClasses().slideUp('100',myopen)
	} else {
		myopen();
	}
	function myopen(){
		var icon = mythis.find('.plus');
		mythis.addClass('active');
		content.addClass('open').slideDown();
		icon.addClass('minus');
	};
	
	jQuery.fn.mycloseClasses = function mycloseClasses(){
		var calcontainer = jQuery(this).parent().parent();
		var icon = calcontainer.find('.minus');
		calcontainer.find('.active').removeClass('active');
		icon.removeClass('minus');
		return this;
	}
		
})
.hover(function(){
		var icon = 	jQuery(this).find('.plus');		
		if(icon.hasClass('minus')){
			return;	
		} else {
			icon.addClass('plus_hover');	
		}			
	},function(){
		jQuery(this).find('.plus').removeClass('plus_hover');
})
//end calendar

// golf club search
jQuery(".centenary_bar a").click(function(){	
	var centenary = jQuery(this);	
	var centenaryblock = jQuery('.centenaries_showhide');
	if(centenary.hasClass('active')){
		centenaryblock.slideUp();
		centenary.removeClass('active');
	} else {
		centenaryblock.slideDown();
		centenary.addClass('active');	
	}
})

// affiliate
jQuery("a.affiliatesblock").click(function(){								 
	var affiliatescontainer = jQuery(this).parent().parent();
	var affiliatesthis = jQuery(this);
	var affiliatescontent = affiliatesthis.next();
	var affiliatesopened = affiliatescontainer.find('.open');
	
	if (affiliatesopened.length > 0) {
		if(affiliatescontent.hasClass('open')){
			affiliatescontent.removeClass('open').mycloseClasses().slideUp('100');
			return false;
		}
		affiliatesopened.removeClass('open').mycloseClasses().slideUp('100',myopen)
	} else {
		myopen();
	}
	function myopen(){
		var affiliatesicon = affiliatesthis.find('.plus');
		affiliatesthis.addClass('active');
		affiliatescontent.addClass('open').slideDown();
		affiliatesicon.addClass('minus');
	};
	
	jQuery.fn.mycloseClasses = function mycloseClasses(){
		var affiliatescontainer = jQuery(this).parent().parent();
		var affiliatesicon = affiliatescontainer.find('.minus');
		affiliatescontainer.find('.active').removeClass('active');
		affiliatesicon.removeClass('minus');
		return this;
	}
		
})
.hover(function(){
		var affiliatesicon = 	jQuery(this).find('.plus');		
		if(affiliatesicon.hasClass('minus')){
			return;	
		} else {
			affiliatesicon.addClass('plus_hover');	
		}			
	},function(){
		jQuery(this).find('.plus').removeClass('plus_hover');
})
//end affiliate

// quiz rule
jQuery("a.quiz_rule").click(function(){								 
	var quizcontainer = jQuery(this).parent();
	var quizthis = jQuery(this);
	var quizcontent = quizthis.next();
	var quizopened = quizcontainer.find('.open');
	
	if (quizopened.length > 0) {
		if(quizcontent.hasClass('open')){
			quizcontent.removeClass('open').mycloseClasses().slideUp('100');
			return false;
		}
		quizopened.removeClass('open').mycloseClasses().slideUp('100',myopen)
	} else {
		myopen();
	}
	function myopen(){
		var icon = quizthis.find('.arrow');
		quizcontent.addClass('open').slideDown();
		quizthis.addClass('quiz_rule_active');
		quizthis.find("span.arrow").html('Close Rule'); 
		icon.addClass('arrow_active');
	};
	
	jQuery.fn.mycloseClasses = function mycloseClasses(){
		var quizcontainer = jQuery(this).parent().parent();
		var icon = quizcontainer.find('.arrow_active');
		quizcontainer.find('.quiz_rule_active').removeClass('quiz_rule_active');
		icon.removeClass('arrow_active');
		quizthis.find("span.arrow").html('View Rule'); 
		return this;
	}
		
})
//end quiz rule

//quiz slide 
//assign handler for quiz
jQuery("#quiz_form").click(function(){
	togglequizform();	
});

//toggle global dropdown panel
function togglequizform(){
	if(jQuery(".quizform").is(":hidden")){
		jQuery(".quizform").slideDown("slow",function(){
			jQuery("#quiz_form").addClass("quiz_form_button_active");
			
		});			  
	}else{
		jQuery(".quizform").slideUp("slow",function(){
			jQuery("#quiz_form").removeClass("quiz_form_button_active");									 
		});				  
	}
}

checkForWrap();

};
// end event binding


//sub nav functions
function closeSubNav() {
    if (typeof (t) != "undefined") {
        clearTimeout(t);
    }
    var ul = jQuery('ul.subnav ul:visible').not('.active, .subopen');
    ul.slideUp();
};

var subnavintent = {
    interval: 50,
    sensitivity: 4,
    over: activate,
    out: subnavout
};

function activate() {
    
    var checkul = jQuery(this).next();
    if ((checkul.is('ul')) && (!checkul.is(':visible'))) {
        var ul = jQuery('ul.subnav ul:visible').not('.active, .subopen');
        ul.slideUp();
        checkul.slideDown('slow', function() {
         });
        return false;
    } 

};

function subnavout() {
    //alert(jQuery(this).attr('class'));
};

function initMenu() {
    jQuery('ul.subnav li a').not('ul.subnav ul li a, .subopen').hoverIntent(subnavintent);
}

//load championship panel
function loadChampionship(loader, championship) {
    jQuery("#championship_" + loader).load("homepage-modules/championships.htm #c_ship_" + championship, function() {
    });
}

function popup(url, winname, w, h, feat) {
    if (!(isNaN(w) || isNaN(h))) {
        var x = parseInt((screen.width - w) / 2);
        var y = parseInt((screen.height - h) / 2);
        if (x < 0) x = 0;
        if (y < 0) y = 0;

        if (feat != null && feat != "") {
            feat = "," + feat;
        }
        else {
            feat = "";
        }
        feat = "left=" + x + ",top=" + y + ",width=" + w + ",height=" + h + feat;
    }
    window.open(url, winname, feat);
}


function helpMe() {
    popup(dotdotpath + "help", "", 900, 700, "");
}



function checkForWrap(){
	jQuery(".blockofthree .module_244").each(function(){
		var titleheight=jQuery(this).find('.module_title').height();
		if(titleheight > 20){
			jQuery(".blockofthree .module_244 .module_title").addClass('dbl_title');
		}
	});
}