/* Author: Bret Morris 2011 */
if(!window.console){console={ log:function(){} }}

$(function(){
	zig = new ZIG ;
});

ZIG = function(){
	this.baseURL = 'http://'+location.hostname;
 	this.page = $('div.framework').attr('id');
	this.loggedIn = (typeof loggedIn != 'undefined') ? loggedIn : false;
  	this.timer;
	this.initialize();
}

ZIG.prototype = {
	initialize:function(){
    var t = this;
		t.skin(t.addListeners);
		t.postAssetLoadIni(t);
	},

	skin:function(c){
    var c = c || null;
    var b = ($.browser.msie) ? 'ie' : ($.browser.safari) ? 'safari' : ($.browser.mozilla) ? 'ff' : 'other';
    
    if(b === "ie"){
    	
    	// add version number to IE
    	b += parseInt($.browser.version, 10);
    	
    }
    	
    $('html').addClass(b);
    
       
    this.updateBarLoadLatestTweet();
    this.sliderFormatting();

	switch(this.page){
		case 'frontpage':
			$(".toolbar a").toolbartip({
      			tip: '#topbar-tips',
      			offset: [10, 2]
      		}).each(function(i){
 				// the code from your post
 				if(this.href && $('html').hasClass('ff'))
					{
					// Remove href-Attributes and set a link class so that the links still looks right
					$(this).data('href', this.href).removeAttr('href').css('cursor', 'pointer');
		
					// Insert the href-Attributes again if clicked
					$(this).click(function() {
						$(this).attr('href', $(this).data('href'));
					});
				}
 			})
      	break;
	}    	
    if(c) c(this);  
  },
  
	addListeners:function(p){
    	var zig = p;
    	
    	$(window).resize(function(){p.resize()});

    	//update bar
    	//$('.update .close').click(function(){p.updateBarHide(this)});
    	
    	//login
    	$(".login").click(function(){p.loginToggle(this)});
    	$('.forgot a').click(function(){ $('.get_password').removeClass('hidden'); })
    	
    	//slider
    	$('.forward').click(function(){p.sliderNext(this)});
    	$('.back').click(function(){p.sliderPrev(this)});
    	$('#slide_nav li a').click(function(){p.sliderNav(this)});
    	
//    	switch(p.page){
//    		case 'tradeEvaluator':
//    			$('.teamName').click(function(){ p.tradeChangeName(this) });
//    			break;
//    	}
  	},

	stopPropagation:function(e){
		var e = e || null;
		if(e) e.stopPropagation();
		return false;	
	},
	
	resize:function(){
	},

	postAssetLoadIni:function(t){
		$(window).load(function(){
			//t.updateBarReveal();
			if($('.login_box #invalid').length > 0) $('.login').click();
		});
	},
	
	formatButtons:function(){
		$('.enhancedButton').button({});
    	$('.groupButtons').buttonset();
    	$('.refresh').button({
    		icons: {
    			primary: 'ui-icon-arrowrefresh-1-w'
    		},
    		text: false
    	});
    	$('.splitsButton').button({
    		icons: {
    			primary: 'ui-icon-gear',
    			secondary: 'ui-icon-newwin'
    		}
    	});
    	$('.selected-sport').addClass('ui-state-active');
    	$('.show-more-cats').button();
    	$('span.help-overlay').addClass('ui-state-highlight');
	},
	
	loginToggle:function(t){
		$(t).toggleClass("selected");
        $(".login_box").toggleClass("preload");
        $("#username").focus();
	},
	/*
	updateBarReveal:function(){
		$('.update').animate({top: '0px'}, 1000);
	},
	
	updateBarHide:function(t){
		$('.update').animate({top: '-' + $('.update').height() + 'px'}, 1000)
	},
	*/
	
	updateBarLoadLatestTweet:function(){
		// set your twitter id
    	var user = 'ziguana';
      
    	// using jquery built in get json method with twitter api, return only one result
   		$.getJSON('http://twitter.com/statuses/user_timeline.json?screen_name=' + user + '&count=1&callback=?', function(data)      {
          
        // result returned
        var tweet = data[0].text;
      
        // process links and reply
        tweet = tweet.replace(/(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig, function(url) {
            return '<a href="'+url+'">'+url+'</a>';
        }).replace(/B@([_a-z0-9]+)/ig, function(reply) {
            return  reply.charAt(0)+'<a href="http://twitter.com/'+reply.substring(1)+'">'+reply.substring(1)+'</a>';
        });
      
        // output the result
        $(".update p").html("<a href='http://twitter.com/ziguana' title='Follow us on Twitter' target='_blank'>@ziguana</a>: " + tweet);
    	}); 
	},
	
	sliderFormatting:function(){
		$('.all_slides').width($('.slide_group').length * $('.slide_group:first').width());
		$('.all_slides').attr('current_slide',1);
	},
	
	sliderNext:function(t){
		var speed = 250;
		var w = $('.slide_group:first').width();
		var a = $('.all_slides');
		var c = a.attr('current_slide');
		if(c < $('.slide_group').length){
			a.animate({
				left: -(c * w) + 'px' }, 
				speed
			).attr('current_slide', parseInt(c) + 1);
			c = a.attr('current_slide');
		} else {
			c = 1;
			a.animate({
				left: 0 }, 
				speed
			).attr('current_slide', c);
		}
		$('#slide_nav li a').removeClass('selected');
		if(c != 1) { 
			$('#slide_nav li a').eq(c - 1).addClass('selected')
		} else { 
			$('#slide_nav li a').eq(0).addClass('selected') 
		}
	},
	
	sliderPrev:function(t){
		var speed = 250;
		var w = $('.slide_group:first').width();
		var a = $('.all_slides');
		var c = a.attr('current_slide');
		if(c == 1){
			a.animate({
				left: -(($('.slide_group').length - 1) * w) + 'px' }, 
				speed
			).attr('current_slide', $('.slide_group').length);
			c = $('.slide_group').length;
		} else {
			a.animate({
				left: -((parseInt(c) - 2) * w) + 'px' }, 
				speed
			).attr('current_slide', parseInt(c) - 1);
			c = a.attr('current_slide');
		}
		$('#slide_nav li a').removeClass('selected');
		if(c != 1) { 
			$('#slide_nav li a').eq(c - 1).addClass('selected')
		} else { 
			$('#slide_nav li a').eq(0).addClass('selected') 
		}
	},

	sliderNav:function(t){
		var t = $(t);
		var speed = 250;
		$('#slide_nav li a').removeClass('selected');
		t.addClass('selected');
		$('.all_slides').animate({
			left: -((t.parent().index()) * $('.slide_group').width()) + 'px'
		}, speed);
		$('.all_slides').attr('current_slide', t.parent().index() + 1);
	},
	
	tradeChangeName:function(t){
		var t = $(t);
		var heldText = t.text();
		var inputHTML = "<input type='text' />";
		if(t.find('input').length == 0) t.html(inputHTML);
		t.find('input').focus();
		t.find('input').blur(function(){
			if(t.find('input').val() == ''){
				t.html(heldText);
			} else {
				t.html(t.find('input').val())
			}
		});
	},
	
	tradeRemovePlayer:function(t){
		var t = $(t);
		var playerList = t.parent();
		t.remove();
		if(playerList.find('li').length == 0) $('.playerResults .helpText').addClass('invisible');
	}

}









































