/* HeadWin-Link Jquery Plugin by Matt Daniels*/
/* REQUIRES jquery.ba-hashchange.min.js to check for hash changes in the URL (for back button functionality) */
/* CSS: .HWlink, .HWlinkBlock, .HWlinkBar, .HWlinkIframe, HWlinkBG, .HWotherLinks, #HWchooseLink, .HWbutton --- .HWbreadcrumb */
/* we need to give it a clickTarget, 2 arrays containing urls/titles for the dropdown, logo url */
/* the optional dropdown menu should default to all target links, if it's activated. It will use the title attribute for the text to display. */
/* in 'full' zoom mode it will look for a 'flag' image - in future versions I should swap the flag for an 'icon' */

(function ($) {
    $.fn.headWin = function (options) {

        var settings = {
            'clickTarget': '.HWlink',
            'logoURL': 'http://horizons.culturewizard.com/Portals/179/logo.png',
			'menu': 'off',
			'menuTarget': 'self',
			'zoom': 'full',
			'source': 'href',
			'flag': 'off',
			'flagSrc': '#CountryTitleFlag',
			'flagTitleSrc': '#CountryTitleName'
        };

        var redir = "#HeadWin";
        var redir2 = "#Home";
		var opString = "";
		
		if (options) {
			$.extend(settings, options);
		}
		var clickTarget = settings['clickTarget'];
		var logoURL = settings['logoURL'];
		var vals = settings['vals'];
		var menu = settings['menu'];
		var menuTarget = ((settings['menuTarget'] == 'self')? $(this) : $(settings['menuTarget']));
		var zoom = settings['zoom'];
		var source = settings['source'];
		var flag = settings['flag'];
		var flagSrc = settings['flagSrc'];
		var flagTitleSrc = settings['flagTitleSrc'];
				
		//Add an array here based on this.each, to create href/title pairs for all the links, replace title with href or the tag's html content if there's no title
		if (menu == 'on') {
			opString = "<span class='HWotherLinks'>Other Links: <select name='HWchooseLink' class='HWchooseLink' id='HWchooseLink'><option value='choose'>-- Choose an Option --</option>";
			menuTarget.each(function () {
				var $this = $(this);
				var href = $(this).attr('href');
				var title = $(this).attr('title');
				if (title == '') { title = href; }
				opString += "<option value='" + href + "'>" + title + "</option>";	
			});
			opString += "</select></span>";
		}
		
		function killAll() { $(".HWlinkIframe").remove; 
		$(".HWlinkBlock").remove(); $(".HWlinkBG").remove(); 
		}
		function closeWin(targetWin) { targetWin.fadeOut('fast', function () { $(".HWlinkBG").fadeOut('fast', function () { location.href = redir2; killAll(); }); }); }
		
        return this.each(function () {
            var $this = $(this);

            
            //plugin code here
            $(this).click(function (e) {
                e.preventDefault();
				killAll();
                $('html, body').animate({ scrollTop: 0 }, 'fast');
				
                var bgContent = "<div class='HWlinkBG " + zoom + "'></div>";
                var allContent = "<div class='HWlinkBlock " + zoom + "' >";
				if (source == 'href') {
					allContent += "<iframe src='" + $(this).attr('href');
					allContent += "' class='HWlinkIframe'></iframe>"; //name='HWlinkIframe'
				} else {
					var htmlcontent = $(this).attr('rel');
					allContent += "<div class='HWlinkIframe'>" + $(htmlcontent).html() + "</div>";
				}
				if (zoom == "full") { 
					allContent += "<span class='HWlinkBar'><img src='" + logoURL + "' />"+opString+"</span><span class='HWbreadcrumb'>"; 
					if (flag == 'on') { 
						var flagZone = $(flagSrc).html();
						var countryTitleZone = $(flagTitleSrc).html();
						allContent += "<span class='flagZone'>"+ flagZone +"</span><span class='countryTitleZone'>"+ countryTitleZone +"</span>"; 
					}
				}
                else {
					allContent += "<span class='HWbreadcrumb'>";
				}
				allContent += "<span class='HWTitle'><span class='HWbutton'></span><span class='HWfButton'></span><span class='HWtTitle'>" + $(this).attr('title') + "</span></span></span>";
				

                $(".HWlinkBlock").hide(); $(".HWlinkBG").hide();
                $(bgContent).hide().appendTo("body").fadeIn('slow');
                $(allContent).hide().appendTo("body").fadeIn('slow', function () { location.href = redir; });
                $(".HWbutton").click(function () {
                    closeWin($(".HWlinkBlock")); //closeWin($(this).parent().parent());
                });
                $(".countryTitleZone").click(function () {
                    closeWin($(".HWlinkBlock")); //closeWin($(this).parent().parent());
                });
				$(".HWlinkBar img").click(function () {
                    closeWin($(".HWlinkBlock")); //closeWin($(this).parent().parent());
                });
				$(".HWlinkBG").click(function () {
                    closeWin($(".HWlinkBlock"));
                });
				$(".HWfButton").click(function () {
					var newLocation = $(".HWlinkBlock iframe").attr("src"); //alert('new loaction: ' + newLocation);
                    killAll(); 
					window.open(newLocation,'_blank');
                });
				
                $(".HWchooseLink").change(function () {
                    //var tFrame = 0;
                    //if (frames.length > 1) { tFrame = frames.length - 1; }
                    var tVal = $("option:selected", this).attr('value');
					var tTitle = $("option:selected", this).text();
                    if (tVal != "choose") { 
						$(".HWlinkBlock iframe").attr("src",tVal);
						$(".HWtTitle").html(tTitle);
					}
                });
            });
            //Set the back button to close our HeadWin
            $(window).hashchange(function () {
                //alert(location.hash); 
                if (location.hash != redir) {
                    closeWin($(".HWlinkBlock"));
                }
            });
            //end
        });



    };
})(jQuery);
    
    
    


