//	default init
//	default is a reserved word so using _default as namespace

(function($) {

    $._default = $._default || {};

    var g_ready = false;
    var g_self = this;

    $.fn._default = function() {
        return this.each(function() {
            //alert("API method called on " + this);
        });
    };

    $._default.init = function(options) {
        var c_self = this;
        var c_ready = false;
        c_self.options = $.extend({}, $._default.init._defaults, options);

        c_self.functions = {
            initialise: function(context) {
                context.each(function(index) {

                });
            },
            flashBanner: function() {
            
				var idName = "banner";
				var elemId = "#" + idName;
            
				var ref = $(elemId).find("a").attr("href");
				var ref = ref.replace("&", "%26")
				var locale = $(elemId).find("img").attr("alt").split("-");
				
				
				//trace(locale)
            
				var flashvars = {
					seeourphones_link: ref,
					lc: locale[0],
					sc: locale[1],
					cc: locale[2]
				};
				var params = {
					menu: "false",
					scale: "noscale",
					wmode: "transparent",
					allowscriptaccess: "always"
				};
				var attributes = {
					name: idName,
					id: idName
				};
				swfobject.embedSWF(preloaderPath, idName, "967", "240", "9.0.0", false, flashvars, params, attributes);
            },
            displayWholeLink: function() {
				$(".feed-story-rep li").click(function(i){
				    var linkHref = $(this).find("a:first").attr("href");
			        location.href= linkHref;
				});
            },
            newsPager: function() {
				
				$(".feed-story-rep").each(function(){

					// create extra markup for scrollable area
					var container = $("<div></div>").addClass("jsScrollHorizontal");
					var navigation = $("<div></div>").addClass("navi");
					
					// wrap and reset container var 
					var container = $(this).wrapInner(container).parent();
					container.append(navigation);
				
					// Init scroller 
					$(this).scrollable({
						size: 3,
						vertical: true,
						next: '.next',
						prev: '.prev',
						items: 'UL',
						naviItem: 'span',
						easing: 'easeOutQuart',
						speed: 500
					});
					
					// Get width and position navi in the center... surely theres a better way of doing this?
					var naviWidth = (navigation.children().width() -3) * navigation.children().length;
					var naviMargin = Math.round(container.width()/2) - naviWidth
					navigation.css("margin-left", naviMargin)
					
					// Remove navi if only 1 item
					if(navigation.children().length <= 1){
						container.addClass("noScroll")
					}
				})
				
            }
        };
        
        c_self.functions.initialise($("body"));
        c_self.functions.flashBanner();
        c_self.functions.newsPager();
        c_self.functions.displayWholeLink();
        c_ready = true;
    };
    
    // This is quick to type: trace("my console text")
    function trace(logtext) {
		try {
			if (console && console.log) {
				console.log(logtext);
			}
		} catch (err) {
			//alert(err);
		}
	};

    // Makes sure each row of the 'beat' section has equal heights
    function beatRowsEqualHeight() {
        var tallest = 0;
        var x = 1;
        $('.feed-beat ul.beat li').each(function() {
            if((x%3) == 0) {
                if(_IE6) {
                    $('.feed-beat ul.beat li:nth-child(' + (x) + ')').height(tallest);
                    $('.feed-beat ul.beat li:nth-child(' + (x-1) + ')').height(tallest);
                    $('.feed-beat ul.beat li:nth-child(' + (x-2) + ')').height(tallest);
                } else {
                    $('.feed-beat ul.beat li:nth-child(' + (x) + ')').css('min-height', tallest);
                    $('.feed-beat ul.beat li:nth-child(' + (x-1) + ')').css('min-height', tallest);
                    $('.feed-beat ul.beat li:nth-child(' + (x-2) + ')').css('min-height', tallest);
                }      
                
                tallest = 0;
            } else {
                if($(this).height() > tallest) {
                   tallest = $(this).height();
                }
            }
            x = x + 1;
        });
    }
    
    // Makes the list item clickable 
    function beatClickableItem() {
        $('.feed-beat ul.beat li').each(function(){
            var theLink = $(this).find('p.feed-link a');
            if(theLink.length != 0) {
                
                $(this).css('cursor', 'pointer');
                $(this).click(function(){
                    window.location.href = theLink.attr('href');
                });
            }
        });
    }

    // _defaults
    $._default.init.defaults = {};

    // initialise one or more instances of the class
    $(document).ready(function() {
        new $._default.init();
        $("body").equalHeights(".feed-story-wrap", ".feed-story-rep", ".feed-rss-wrap", ".feed-rss-rep");
        
        beatRowsEqualHeight();
        beatClickableItem();

    });
})(jQuery);