/*-------------------------------------------------------------------------------------------------*
 *  DEFAULT by AM Design
 *
 *  JavaScript functions for the "Default" theme by AM Design, built for the AM Design Content Management 
 *	System
 *
 *  Last Updated:  2/2011
 *  Updated By:  Jake Thompson
 *-------------------------------------------------------------------------------------------------*/

/* Global Variables */
var BASEURL, ADMINURL, COREURL, SITEURL, MEDIAURL, THEMEURL, THE_URL;
var BASEPATH, ADMINPATH, COREPATH, SITEPATH, MEDIAPATH, THEMEPATH;
var timeouts = [];

$(document).ready(function(){
	setBrowserClass();
	$(".green_btn").each(function() {
		var el_id = $(this).getUID();
		$("#"+el_id).wrapInner("<span class='green_btn_middle'></span>");
		$("<span class='green_btn_open'></span>").insertBefore($("#"+el_id+" .green_btn_middle"));
		$("<span class='green_btn_close'></span>").insertAfter($("#"+el_id+" .green_btn_middle"));
		$("#"+el_id).after("<span class='clear'></span>");
	});
	
	$(".green_btn_smaller").each(function() {
		var el_id = $(this).getUID();
		$("#"+el_id).wrapInner("<span class='green_btn_middle'></span>");
		$("<span class='green_btn_open'></span>").insertBefore($("#"+el_id+" .green_btn_middle"));
		$("<span class='green_btn_close'></span>").insertAfter($("#"+el_id+" .green_btn_middle"));
		//$("#"+el_id).after("<span class='clear'></span>");
	});
	
	$('.grey_rounded').each(function() {
		$(this).append('<div class="clear"></div>');
	});
	
	//$('#registration_form').jqTransform();
	//$('#registration_payment').jqTransform();
	init_datepicker();
	
	$('#rotator_images').cycle({ 
		fx:			'scrollLeft',
		containerResize: 0,
		slideResize: 0,
		speed:		1000, 
		timeout:	8000,
		animIn: {
			opacity: 1
		},
		animOut: {
			opacity: 0
		},
		pager:		"#rotator_nav_links",
		before:		onBefore,
		after:		checkWidth,
		
		pagerAnchorBuilder: function(idx, slide) {
			var newid=parseInt(idx)+1;
			var newhtml = $("#page-1"+newid).html();
			return '<li id="page-1'+newid+'">'+newhtml+'</li>'; 
		} 
	});
	
	$("#paging #nav li a").click(function() { $('#rotator_images').cycle('pause'); });
	
	init_site();
	if($('body').hasClass('home')) {
		home_footer();
	}
});

$(window).resize(function() {
	checkWidth();
	home_footer();
});

function stop_slideshow() {
	$('#rotator_images').cycle('stop');
}

function init_site(){
	// Initialize UI functionality (e.g. navigation hover)
	$('#nav>li').hover(
		function(){
			var navID = $(this).attr('id');
			clearTimeout( timeouts[navID] );
			$(this).children('ul').fadeIn(200);
		},
		function(){
			var navID = $(this).attr('id');
			clearTimeout( timeouts[navID] );
			var to = setTimeout("$('#"+navID+" ul').fadeOut(200);", 150);
			timeouts[navID] = to;
		}
	);
	
	custom_on_resize();
	window.onresize = custom_on_resize;
	
	clearClick();
}

function css_tweaks(){
	
}

function custom_on_resize(){
	var screen_size = getScreenSize();
	var screen_height = screen_size[1];
	var body_height = $('body').height();
	if( body_height < screen_height ){
		$('html, body').css('height','100%');
	}else{
		$('html, body').css('height','auto');
	}
}

(function($) {
    var uid = 0;
    $.getUID = function() {
        uid++;
        return 'jQ-uid-'+uid;
    };
    $.fn.getUID = function() {
        if(!this.length) {
            return 0;
        }
        var fst = this.first(), id = fst.attr('id');
        if(!id) {
            id = $.getUID();
            fst.attr('id', id);
        }
        return id;
    }
})(jQuery);

function onBefore(curr, next, opts) {
	$("#rotator_nav").css("display", "none");
	$(curr).removeClass('activeSlide');
	$(next).addClass('activeSlide');
	checkWidth();
}

function checkWidth() {
	var screen_size = getScreenSize();
	var width = screen_size[0] - 17;
	$('#rotator_images').css('width', width);
	//$('.activeSlide').css('width', width);
	$('.rotator_box').css('width', width);
	$('.rotator_box').each(function() {
		$(this).css('width', '');
		$(this).css('width', width + 'px !important');
	});
	//$('.rotator_box:not(.activeSlide)').css('left','-'+width);
	//$('.activeSlide').css('left',0);
}

function home_footer() {
	if($('body').hasClass('home')) {
		var screen_size = getScreenSize();
		var height = screen_size[1];
		var fHeight = height - 625;
		$('#footer').css('height', fHeight);
	}
}

// REGISTRATION

var LEVEL_FREE, LEVEL_BASIC, LEVEL_PRO, LEVEL_ISO;

function init_website_popup( target, url, autorun, callback ){
	callback = callback || function(){};
	$(target).fancybox({
		href: url,
		modal: true,
		padding: 0,
		titleShow: false,
		transitionIn: 'none',
		transitionOut: 'none',
		onComplete : callback
	});
	
	if( autorun ) $(target).trigger('click');
}

function init_popup_content(){
	$(".popup .green_btn").each(function() {
		var el_id = $(this).getUID();
		$("#"+el_id).wrapInner("<span class='green_btn_middle'></span>");
		$("<span class='green_btn_open'></span>").insertBefore($("#"+el_id+" .green_btn_middle"));
		$("<span class='green_btn_close'></span>").insertAfter($("#"+el_id+" .green_btn_middle"));
		$("#"+el_id).after("<span class='clear'></span>");
	});
	Cufon.replace('.popup .top .title', { fontFamily:'Dobra Medium' });
	Cufon.replace('.popup .green_btn', { fontFamily:'DIN Medium', color: '-linear-gradient(#5C5B5B, #333333)' });
}

function force_numeric( selector ){
	$(''+selector).keyup(function(){
		var val = $(this).val().trim();
		if(val.length > 0){
			var lastChar = val.substring(val.length - 1);
			if( !is_numeric(lastChar) && lastChar != '.' ){
				$(this).val(val.substring(0, val.length - 1));
			}
		}
	});
}

function register_level(level){
	$('input#reg_level').val(level);
	$('#registration_form').submit();
}

function register_iso_level(){
	var user_licenses = $('#iso_user_licenses').val();
	var is_custom = parseInt( $('#iso_is_custom').val() );
	if( is_custom == 1 ){
		user_licenses = $('#iso_custom_user_licenses').val();
	}
	$('#reg_user_licenses').val( user_licenses );
	$('#reg_is_custom').val( is_custom );
	$('input#reg_level').val(LEVEL_ISO);
	$('#registration_form').submit();
}

function toggle_custom_iso_licenses(){
	var user_licenses = $('#iso_user_licenses').val();
	if( user_licenses == 'CUSTOM' ){
		$('#iso_is_custom').val(1);
		$('#custom_licenses_wrap').show();
	}else{
		$('#custom_licenses_wrap').hide();
		$('#iso_is_custom').val(0);
		$('#iso_custom_user_licenses').val('');
	}
}

function init_datepicker(){
	// Publish Date
	var options = {
		dateFormat: 'MM d, yy',
		showAnim: 'fadeIn',
		showOn: 'both',
		buttonImage: '/am-admin/images/icons/calendar.png',
		buttonImageOnly: true,
		changeYear: true
	};
	$('.datepicker').datepicker(options);
} 

function submit_step(id) {
	$('#' + id).submit();	
}

function custom_user_field() {
	var val = $('#user_limit').val();
	if(val == 'custom' || parseInt(val) == 250) {
		$('.custom_users').show();
	} else {
		$('.custom_users').hide();	
	}
	
	if(parseInt(val) == 250) {
		$('.custom_users label').text('Amount of Users Needed (in bundles of 25):');	
	} else {
		$('.custom_users label').text('Amount of Users Needed:');	
	}
}
