$(function(){
	// ==================================
	// Set Auto Height for iFrame
	// ==================================
	var h = $(window).height();
    h = h - 48;
	
    $(window).bind("resize", function(){
		var new_h = $(window).height();
        new_h = new_h - 48; 
        $('#themebaker_frame').height(new_h);
	});

    $('#themebaker_frame').height(h);
	
	// ==================================
	// Init
	// ==================================
	var url = get_url_vars(), theme = "", current_theme = "";
	
	if(url['theme'] != "" && url['theme'] != undefined){
		
		current_theme = url['theme'];	
		
		$("#theme option").each(function(){
			var self = $(this);
			
			if(self.val() == current_theme){
				self.attr('selected', 'selected');
			} else {
				self.attr('selected', '');	
			}
		});
		
		$("#themebaker_frame").attr("src", "http://demo.themebaker.com/" + current_theme);
		
		$(".btn.get_this_theme").attr("href",  "http://themebaker.com/" + current_theme);
	}
	
	$("#themebaker_frame").show();
	document.title = 'ThemeBaker Demos - ' + current_theme.toUpperCase();
	
	// ==================================
	// Theme Picker
	// ==================================
	$("#theme").live("change", function() {
		var self = $(this);
		var theme = self.has(':selected').val();
			
		$("#themebaker_frame").attr("src", "http://demo.themebaker.com/" + theme);
		
		$(".btn.get_this_theme").attr("href",  "http://themebaker.com/" + theme);
		
		document.title = 'ThemeBaker Demos - ' + theme.toUpperCase();
		window.location.hash = "#theme=" + theme;
		
	});
	
	// ==================================
	// Close Frame
	// ==================================
	$(".close-frame").bind("click", function(e) {
		e.preventDefault();
		window.location = $("#themebaker_frame").attr("src");
	});
});

// ==================================
// Get URL hash values
// ==================================
function get_url_vars(){
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('#') + 1).split('&');
 
    for(var i = 0; i < hashes.length; i++) {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }
 
    return vars;
}


