/**
 * ozLancer Pro
 *
 * Copyright (c) 2011 OzLancer Pro (ozlancer.com)
 * All terms, conditions and copyrights as defined
 * in the ozLancer License Agreement
 * http://www.ozlancer.com/
 *
 */
 
$(document).ready(function() { 	
	$('input,select,textarea').focus(function(){ $(this).addClass('active'); }).focusout(function(){  $(this).removeClass('active');  });
	$('a[rel*=facebox]').facebox();
	$('.tooltip').tooltip();

	/* Action: Provide feedback */
	$('.provide_feedback').click(function(){
            $.facebox(function() {
                $.get(base_url+'/cms/feedback/remote/', function(data) {
                    $.facebox(data);
                    $('#facebox .form_provide_feedback').validate({
                        onkeyup: false,
                        submitHandler: function(form) {
                            button_loading($(form).find('button.submit'), true);
                            form.submit();
                        }
                    });
                });
            });
	});		

	/* CMS - Activity Feed: Strip out empty links */
	$('.feed_about a').each(function(){
		if ($(this).attr('href').length <=0)
			$(this).removeAttr('href').replaceWith('<span>'+$(this).text()+'</span>');
	});
	
	
});
function load_ratings(el)
{
    $(el).each(function() { $(this).raty({  readOnly:  true,  start: $(this).attr('rel'), showHalf:  true }); });
}
function popup_remote(url)
{
	$.facebox({ajax:url});
}

function show_more_text(el)
{
	$(el).hide().next('.show_more_text').show();
}

function toggle_text(jqElement, text1, text2)
{ 
	jqElement.text(jqElement.text() == text1 ? text2 : text1); 
}

function submit_handler() 
{
    button_loading('#id_submit',true);
    window.submit_handle = function(evt) {
        evt.preventDefault();
    }
    return true;
}

function button_location(obj_id, loc)
{    
    button_loading(obj_id, true);    
    window.location = loc;
    return false;
}

function button_loading(obj_id,enable_switch)
{
    if (enable_switch)
    {
        var float_orientation = $(obj_id).css('float');
        $(obj_id).attr('disabled','disabled').wrap($("<span></span>").addClass('button_loading').addClass('button_loading_'+float_orientation)).fadeTo('fast', 0.6);        
    }
    else
    {
        $(obj_id).attr('disabled',false).unwrap().fadeTo('fast', 1);
    }
}

/* Slim Tabs */
function slim_tabs(tab_class) {
    var hash = window.location.hash;

    $(tab_class+"-link").click(function() {
        var tab_ref = $(this).attr('rel');
        slim_tabs_show("#"+tab_ref, tab_class);
    });

    $(tab_class+"-tab").hide();
    slim_tabs_show(hash, tab_class);
    return false;
}
function slim_tabs_show(tab_ref, tab_class)
{
    if ($(tab_ref+"-show").length > 0)
    {
        $(tab_class+' a').each(function(){
            var tab_id = "#"+$(this).attr('rel');
            if (tab_id == tab_ref)
            {
                $(this).parent().children(tab_class+"-link").each(function(){
                    var get_ref = $(this).removeClass('selected').attr('rel');
                    $("#"+get_ref+"-show").hide();
                });
                $(this).addClass('selected'); 
            }
        });
        $(tab_ref+"-show").show();
    }
    else
    {
        $(tab_class).each(function() {
                var get_ref = $(this).children(tab_class+"-link:first").addClass('selected').attr('rel');
                $("#"+get_ref+"-show").show();
        });
    }
}

/* Build Steps List */
function steps_list()
{
    $('.steps_list li').not('.steps_list li:last').append('<span class="wedge">&nbsp;</span>');
    $('.steps_list li.selected').prev('li').addClass('selected previous-last').find('span.wedge').addClass('selected previous-last');
    $('.steps_list li.selected').prev('li').prev('li').addClass('selected previous').find('span.wedge').addClass('selected previous');
    
}

/* Detect Timezone */
// script by Josh Fraser (http://www.onlineaspect.com)
function time_zone_calculate() {
	var rightNow = new Date();
	var jan1 = new Date(rightNow.getFullYear(), 0, 1, 0, 0, 0, 0);  // jan 1st
	var june1 = new Date(rightNow.getFullYear(), 6, 1, 0, 0, 0, 0); // june 1st
	var temp = jan1.toGMTString();
	var jan2 = new Date(temp.substring(0, temp.lastIndexOf(" ")-1));
	temp = june1.toGMTString();
	var june2 = new Date(temp.substring(0, temp.lastIndexOf(" ")-1));
	var std_time_offset = (jan1 - jan2) / (1000 * 60 * 60);
	var daylight_time_offset = (june1 - june2) / (1000 * 60 * 60);
	var dst;
	if (std_time_offset == daylight_time_offset) {
		dst = "0"; // daylight savings time is NOT observed
	} else {
		// positive is southern, negative is northern hemisphere
		var hemisphere = std_time_offset - daylight_time_offset;
		if (hemisphere >= 0)
			std_time_offset = daylight_time_offset;
		dst = "1"; // daylight savings time is observed
	}
	var i;	
	
	return time_zone_convert(std_time_offset)+","+dst;
}

function time_zone_convert(value) {
	var hours = parseInt(value);
   	value -= parseInt(value);
	value *= 60;
	var mins = parseInt(value);
   	value -= parseInt(value);
	value *= 60;
	var secs = parseInt(value);
	var display_hours = hours;
	// handle GMT case (00:00)
	if (hours == 0) {
		display_hours = "00";
	} else if (hours > 0) {
		// add a plus sign and perhaps an extra 0
		display_hours = (hours < 10) ? "+0"+hours : "+"+hours;
	} else {
		// add an extra 0 if needed 
		display_hours = (hours > -10) ? "-0"+Math.abs(hours) : hours;
	}
	
	mins = (mins < 10) ? "0"+mins : mins;
	return display_hours+":"+mins;
}

/* Convert int to money */
function num_money(n_value) {
	// validate input
	if (isNaN(Number(n_value)))
	return 'ERROR';
	

	if (n_value == 1000) return '1,000.00';
	if (n_value == 1) return '1.00';
	
	// save the sign
	var b_negative = Boolean(n_value < 0);
	n_value = Math.abs(n_value);
	
	// round to 1/100 precision, add ending zeroes if needed
	var s_result = String(Math.round(n_value*1e2)%1e2 + '00').substring(0,2);
	
	// separate all orders
	var b_first = true;
	var s_subresult;
	while (n_value > 1) {
	s_subresult = (n_value >= 1e3 ? '00' : '') + Math.floor(n_value%1e3);
	s_result = s_subresult.slice(-3) + (b_first ? '.' : ',') + s_result;
	b_first = false;
	n_value = n_value/1e3;
	}
	// add at least one integer digit
	if (b_first)
	s_result = '0.' + s_result;
	
	// apply formatting and return
	return b_negative
	? '(' + s_result + ')'
	: '' + s_result;
}

function num_time(n_value) {
	var s_result;
	var s_ampm = "AM";

	var v_hour = Math.floor(n_value);
	var v_minutes = 60*(n_value - v_hour);
	
	var d = new Date(1970, 0, 0, v_hour, v_minutes, 0);
	var hours = d.getHours()
	var minutes = d.getMinutes()

	if(hours > 11){	
		hours = hours-12;
		s_ampm = "PM";
	}
	
	if (minutes < 10){
		minutes = "0" + minutes
	}

	if (hours == 0){
		hours = 12;
	}

	s_result = hours + ":" + minutes + " "+s_ampm;
	return s_result;
	
}

