var addthis_config = {
  "data_track_clickback":true,
  ui_cobrand: "Shisha",
  ui_header_background: "#d300bb",
  ui_header_color: "#ffffff"
};

$(function() {
	$("body").addClass("jsEn");
	
	$("div#header p.text-size-controls a").click(function() {
		$.getJSON(MIYOLib._conf.siteBaseURL+'/accessibility/textsize/' + $(this).attr("class") + '/?mode=ajax', function(data) {
			$("body")
				.removeClass("small")
				.removeClass("medium")
				.removeClass("large")
				.addClass(data.text_size);
		});
			
		return false;
	});
	
  $("a[href^=http://]")
  	.not("[href^="+MIYOLib._conf.siteBaseURL+"]")
  	.each(function() {
  			this.target = "_blank";
  	});
  
  $("a.external").each(function() {
    this.target = "_blank";
  });
	
	doBannerReplace();
	handleKeyboardPrimaryNav();
	placeYouTube();
	initExpandableSections();
});

function doBannerReplace() {
  if($.flash.available) {
    $(".header-bar-decorative").each(function() {
      headBarDecRef = this;
      headBarDecRef.$holdingContainer = $(headBarDecRef).closest("#page-header-bar");
      if(!headBarDecRef.$holdingContainer.hasClass("flash-enabled")) {
        headBarDecRef.$holdingContainer.addClass("flash-enabled");
      }
      headBarDecRef.$spanConfig = $(headBarDecRef).find("span:first");
      headBarDecRef.useGeneric = true;
      headBarDecRef.banSize = 'large';
      if(headBarDecRef.$holdingContainer.hasClass("smaller-bar")) {
        headBarDecRef.banSize = 'small';
      }
      headBarDecRef.genericSWF = 'generic_'+headBarDecRef.banSize;
      headBarDecRef.custBanner = '';
    
      if(headBarDecRef.$spanConfig.length > 0) {
        var targetSWFID = headBarDecRef.$spanConfig.attr("id");
        if(targetSWFID) {
          var targetSWF = targetSWFID.substring((targetSWFID.lastIndexOf("-") + 1), targetSWFID.length);
          if(targetSWF) {
            headBarDecRef.useGeneric = false;
            headBarDecRef.custBanner = targetSWF;
          }
        }
      }
      
      if(headBarDecRef.useGeneric) {
        if(headBarDecRef.banSize == 'large') {
          $(headBarDecRef).flash({ width:572, height:120, swf: MIYOLib._conf.siteBaseURL+'/flash/banners/'+headBarDecRef.genericSWF+'.swf', allowFullScreen: false, allowscriptaccess: 'always', wmode: 'transparent' });
        } else {
          $(headBarDecRef).flash({ width:240, height:120, swf: MIYOLib._conf.siteBaseURL+'/flash/banners/'+headBarDecRef.genericSWF+'.swf', allowFullScreen: false, allowscriptaccess: 'always', wmode: 'transparent' });
        }
      } else {
        if(headBarDecRef.banSize == 'large') {
          $(headBarDecRef).flash({ width:572, height:120, swf: MIYOLib._conf.siteBaseURL+'/flash/banners/'+headBarDecRef.custBanner+'.swf', allowFullScreen: false, allowscriptaccess: 'always', wmode: 'transparent' });
        } else {
          $(headBarDecRef).flash({ width:240, height:120, swf: MIYOLib._conf.siteBaseURL+'/flash/banners/'+headBarDecRef.custBanner+'.swf', allowFullScreen: false, allowscriptaccess: 'always', wmode: 'transparent' });
        }
      }
    });
  }
}

function handleKeyboardPrimaryNav() {
  $("#primary-nav").children("li").children("a").each(function() {
	  var currLinkRef = this;
	  currLinkRef.$parentLi = $(currLinkRef).closest("li");
	  currLinkRef.$subNav = currLinkRef.$parentLi.find(".subnav-dropdown");
	  
	  $(currLinkRef).focus(function() {
	    currLinkRef.$parentLi.addClass("over");
	  });
	  
	  if(currLinkRef.$subNav.length > 0) {
	    currLinkRef.$subNav.find("a:last").each(function() {
	      var currSelSubLink = this;
	      
	      $(currSelSubLink).blur(function() {
	        currLinkRef.$parentLi.removeClass("over");
        });
	    });
	  } else {
	    $(currLinkRef).blur(function() {
  	    currLinkRef.$parentLi.removeClass("over");
  	  });
	  }
	  
	  $(currLinkRef).mouseenter(function() {
	    $("#primary-nav").children("li").each(function() {
	      $(this).removeClass("over");
	    });
	  });
	  
	  $(currLinkRef).click(function() {
	    $("#primary-nav").children("li").each(function() {
	      $(this).removeClass("over");
	    });
	    
	    if(!$(this).hasClass("is-home-nav")) {
	      return false;
      }
	  });
	});
}

function placeYouTube() {
  if($.flash.available) {
    $(".youtube-replace").each(function() {
      var selfRef = this;
    
      selfRef.$vidInfo = $(selfRef).siblings(".youtube-vid-info");
      selfRef.$parentDiv = $(selfRef).parent();
    
      if(selfRef.$vidInfo.length > 0) {
        selfRef.vidID = selfRef.$vidInfo.attr("id");
      
        selfRef.$flashRef = $(selfRef).parent().flash({
          width:480,
          height:295,
    			swf: 'http://www.youtube.com/v/'+selfRef.vidID+'?enablejsapi=1&amp;hl=en_GB&amp;fs=1',
    			allowFullScreen: true,
    			allowscriptaccess: 'always',
    			wmode: 'transparent'
        });
      
        selfRef.$parentDiv.find("object").addClass("youtube-vid");
      }
    });
  }
}

function initExpandableSections() {
  $(".expandable-content").each(function() {
    var parentHold = this;
    parentHold.collapseOthers = false;
    
    if($(parentHold).hasClass("collapse-others")) {
      parentHold.collapseOthers = true;
      parentHold.$otherExpandables = $(parentHold).parent().find(".expandable-content");
    }
    
    parentHold.$blockHead = $(parentHold).find(".expand-header:first, .expand-trigger").not(".sub-expandable .expand-header")
      .css("cursor", "pointer")
      .click(function() {
        var headerRef = this;
        headerRef.originallyHadClass = true;
        
        if(!$(parentHold).hasClass("open")) {
          $(parentHold).find(".initial-content").css("display", "none");
          headerRef.originallyHadClass = false;
          
          if(parentHold.collapseOthers) {
            if(parentHold.$otherExpandables.length > 0) {
              parentHold.$otherExpandables.each(function() {
                var expandElement = this;
                
                if($(expandElement).hasClass("open")) {
                  $(expandElement).find(".expanded-content:first").slideToggle(600, function() {
                    $(expandElement).removeClass("open");
                    $(expandElement).find(".initial-content").css("display", "block");
                  });
                }
              });
            }
          }
        }
        
        $(parentHold).find(".expanded-content:first").slideToggle(600, function() {
          if(headerRef.originallyHadClass && $(parentHold).hasClass("open")) {
            $(parentHold).removeClass("open");
            $(parentHold).find(".initial-content").css("display", "block");
          } else if(!headerRef.originallyHadClass && !$(parentHold).hasClass("open")) {
            $(parentHold).addClass("open");
          }
        });
        
        return false;
      });
  });
  
  $(".sub-expandable").each(function() {
    var subExpandHold = this;
    subExpandHold.collapseOthers = false;

    if($(subExpandHold).hasClass("collapse-others")) {
      subExpandHold.collapseOthers = true;
      subExpandHold.$otherExpandables = $(subExpandHold).parent().find(".sub-expandable");
    }

    subExpandHold.$blockHead = $(subExpandHold).find(".expand-header:first, .expand-trigger")
      .css("cursor", "pointer")
      .click(function() {
        var subHeaderRef = this;
        subHeaderRef.originallyHadClass = true;

        if(!$(subExpandHold).hasClass("open")) {
          $(subExpandHold).find(".initial-content").css("display", "none");
          subHeaderRef.originallyHadClass = false;
          
          if(subExpandHold.collapseOthers) {
            if(subExpandHold.$otherExpandables.length > 0) {
              subExpandHold.$otherExpandables.each(function() {
                var expandElement = this;

                if($(expandElement).hasClass("open")) {
                  $(expandElement).find(".sub-expand-content:first").slideToggle(600, function() {
                    $(expandElement).removeClass("open");
                    $(expandElement).find(".initial-content").css("display", "block");
                  });
                }
              });
            }
          }
        }

        $(subExpandHold).find(".sub-expand-content:first").slideToggle(600, function() {
          if(subHeaderRef.originallyHadClass && $(subExpandHold).hasClass("open")) {
            $(subExpandHold).removeClass("open");
            $(subExpandHold).find(".initial-content").css("display", "block");
          } else if(!subHeaderRef.originallyHadClass && !$(subExpandHold).hasClass("open")) {
            $(subExpandHold).addClass("open");
          }
        });

        return false;
      });
  });
}
