$(document).ready(function () {
	initLB();
	initImgLB();
	//initAutoComp();
	initMediaViewer();
	mediaViewer.rotateMedia();
	set_chartab_hover();
	
	$('#btn_advanced').click(function() {
		toggleAdvanced();
	});
	initCharTabs();
	initStatSelector();
	$('.achievement_list').hide();

	$('.toggle_achiev').click(function() {
		toggle_achiev(this);	
	});
	$('.toggle_criteria').click(function() {
		toggle_criteria(this);	
	});
});


function initImgLB() {
	img_lb = new GalleryLightBox();
}

function initLB() {
	lightbox = new LightBox();
}

function initAutoComp() {
	var data = "Level 80 Human Warrior,Level 80 Dwarf Warrior,Level 80 Gnome Rogue,Level 80 Undead Mage,Level 80 Human Death Knight".split(",");
	$("#search").autocomplete(data);
}

function LightBox() {
	this.openLB = function (url) {
		$('body').append("<div id='lb_bg'></div><div id='lb_content'></div>");
		window.scrollTo(0,0)
		$('#lb_bg').click(function(){
			lightbox.closeLB(); 
		});


		$('#lb_content').load(url, function() {
			var w = $('#lb_content').width() / 2;
			var h = $('#lb_content').height() / 2;
			$('#lb_content').css('margin', "-"+h+"px 0 0 -"+w+"px");
		});


	};

	this.closeLB = function () {
		$('#lb_bg').remove();
		$('#lb_content').remove();		
	};



}

function GalleryLightBox() {
	this.gallery_index = 0;
	this.srcArray = ['/static/images/wow1.jpg', '/static/images/wow2.jpg', '/static/images/wow3.jpg']
	this.openLB = function (index) {
		$('body').append("<div id='lb_bg'></div><div id='lb_content'></div>");
		window.scrollTo(0,0)
		$('#lb_bg').click(function(){
			lightbox.closeLB(); 
		});
		var img = $("<img id='cur_img' src='"+this.srcArray[index]+"' /> <div id='scroll_btns'><em onclick='img_lb.prev_img()'>Left</em><span>|</span><em onclick='img_lb.next_img()'>Right</em></div>");
		$(img).load(function() {
			$('#lb_content').append(img)	
				img_lb.position_content();	
		});		
		
	
		this.gallery_index = index;



	}

	this.position_content = function() {
		var w = $('#lb_content').width() / 2;
		var h = $('#lb_content').height() / 2;
		$('#lb_content').css('margin', "-"+h+"px 0 0 -"+w+"px");
		var btns_w = $('#scroll_btns').width() / 2;
		$('#scroll_btns').css('margin', "0 0 0 -"+btns_w+"px");		
	}

	this.next_img = function () {
		if(this.gallery_index == this.srcArray.length - 1)
			this.gallery_index = 0
		else
			this.gallery_index++;
		this.refresh_img(this.gallery_index)
	}

	this.prev_img = function () {
		if(this.gallery_index == 0)
			this.gallery_index = this.srcArray.length - 1
		else
			this.gallery_index--;
		this.refresh_img(this.gallery_index)
	}
	
	this.refresh_img = function (i) {
		var img = $("<img id='cur_img' src='"+this.srcArray[i]+"' />")
		$(img).load(function() {
				$('#cur_img').replaceWith(img);
				img_lb.position_content();
		});
	}	

	this.closeLB = function () {
		$('#lb_bg').remove();
		$('#lb_content').remove();		
	};

}

function toggleAdvanced() {
	var as = $('#adv_search');
	if (as.css('display') != 'block') {
		as.show('slow');
	}
	else {
		as.hide('slow');
	}	
}

function initMediaViewer() {
	mediaViewer = new MediaViewer();
	$('.news_thumb').each(function(i){
		this.onclick = function () { 
			mediaViewer.viewStory(i);
		}
	});
}


function MediaViewer() {
	this.mediaIndex = 0;
	this.rotate = true;
	this.media = $('.news_entry');
	this.media_thumbs = $('.news_thumb');	
	this.rotateMedia = function() {
		if (this.rotate) {
			if (this.media_thumbs.length <= 1) {
				return;
				$('.news_thumb').css('opacity', '1');
			}
			if (this.mediaIndex < this.media.length) {
				this.hideMedia();
				$(this.media[this.mediaIndex]).fadeIn('slow');			
				this.setBtnDefault();
				this.media_thumbs[this.mediaIndex].style.opacity = '1';
				//$(this.media_thumbs[this.mediaIndex]).animate({opacity:'1'}, 500);				
				this.mediaIndex++;
				setTimeout('mediaViewer.rotateMedia()', 5000);
				if (this.mediaIndex == this.media.length)
						this.mediaIndex = 0;			
			}
		}
	};
	
	this.viewStory = function(index) {
		this.rotate = false;
		this.hideMedia();
		this.media[index].style.display = 'block';	
		this.setBtnDefault();
		this.media_thumbs[index].style.opacity = '1';		
	};
	
	this.hideMedia = function () {
		for ( i = 0; i < this.media.length; i++) {
			this.media[i].style.display = 'none';		
		}
	}
	
	this.setBtnDefault = function () {
		for ( i = 0; i < this.media_thumbs.length; i++) {
			this.media_thumbs[i].style.opacity = '.5';		
		}
	}
}


function hideItems() {
	$('.gear_list li img').hide();
}

function get_ext(url) {
	var split_url = url.split('.');
	var ext = split_url[split_url.length - 1];
	return ext;
}

function initCharTabs() {
    charTabs = new CharTabs();

    $('#tab_stats').click(function() {
        charTabs.selectTab('char_gear_stats');
        charTabs.highlightTab(this);
    });   
    $('#tab_rep').click(function() {
        charTabs.selectTab('char_rep');
        charTabs.highlightTab(this);
    });      
    $('#tab_achiev').click(function() {
        charTabs.selectTab('char_achiev');
        charTabs.highlightTab(this);
    });     
    $('#tab_descr').click(function() {
        charTabs.selectTab('char_description');
        charTabs.highlightTab(this);
    });   
}

function CharTabs() {
    this.selectTab = function(id) {
        this.hideAll();
        $('#'+id).show();   
    }

    this.hideAll = function() {
        $('.char_info').hide();
    }
   
    this.highlightTab = function (el) {
        $('#char_tabs li').removeClass();
        $(el).addClass('selected');
    }
   
}

function initStatSelector() {
    statSelect = new StatSelector();
}

function StatSelector() {
	
    this.selectStat = function(opt) {
		var id = opt.getAttribute('stat');
        this.hideAll();

        $('#'+id).show();   
    }

    this.hideAll = function() {
        $('.stat_list').hide();
    }

}


function toggle_class(el) {
	var c = $(el).parent().parent().find('tbody tr');
	if ($(c).css('display') != 'none') {
		$(c).hide();
		$(el).find('.exp_col').html('click to <b>show</b>');
	}
	else {
		$(c).show();
		$(el).find('.exp_col').html('click to <b>hide</b>')
	}
	
}

function toggle_achiev(el) {
	var a = $(el).parent().find('.achievement_list');
	var btn = $(el).parent().find('em.toggle_achiev');
	if (a.css('display') != 'none') {
		a.hide()
		$(btn).text('[+]');
	}
	else {
		a.show();
		$(btn).text('[-]');
	}
}

function toggle_criteria(el) {
	var criteria = $(el).find('.achiev_criteria');
	if (criteria.css('display') != 'none') {
		criteria.hide()
	}
	else {
		criteria.show();
	}
}

function toggle_shipping(el) {
	if(el.checked) {
		$('#shipping_info').hide();
	}
	else {
		$('#shipping_info').show();
	}
}

function set_chartab_hover() {
	if ($('#char_tabs').length) {
		$('#char_tabs li').hover(
			function() {
				$(this).css('background-color', '#6B868F')
			},
			function() {
				$(this).css('background-color', '#8EA7AF')
			}
		);	
	}
	else { return;}
}