//
//// Global js using the jquery library

jQuery(document).ready(function(){

	//$.ajax({ url: "test.html", context: document.body, success: function(){
//        $(this).addClass("done");
//      }});
return;
	// create new releases object array
	var xmlfile = 'http://anaphylaxistheplay.com/wordpress/wp-content/themes/baltimore-phototheme/bpt.php';
	// Open the XML file
	$.get(xmlfile,{'func': 'orders'},function(xml){
	  	jQuery('#post-178 .entry').prepend('<p class="bpt_last_five">Last two days:</p><table border="0" cellpadding="0" cellspacing="0" width="100%" id="tix_orders_list"></table>');
			jQuery('#tix_orders_list').append('<tr><th>date ordered</th><th>date ID</th><th>num tix</th><th>First Name</th><th>Last Name</th></tr>');
		//jQuery('#tix_orders_list').after(xml);
		// Run the function for each video tag in the XML file
		today = new Date();
		var five_days = new Date(today.getFullYear(),today.getMonth(),today.getUTCDate()-2);
		var five_days = get_simple_date(strtotime(five_days.toDateString()));
		$('item',xml).each(function(i) {
			// need a variable for each possible option in the xml
			bpt_order_time = get_formatted_date(strtotime($(this).find("bpt_order_time").text()));
			bpt_simple_time = get_simple_date(strtotime($(this).find("bpt_order_time").text()));
			bpt_date_id = $(this).find("bpt_date_id").text();
			bpt_quantity = $(this).find("bpt_quantity").text();
			bpt_fname = $(this).find("bpt_fname").text();
			bpt_lname = $(this).find("bpt_lname").text();
			price_collected_value = $(this).find("bpt_price_collected_value").text();
			if(bpt_simple_time>five_days){
				jQuery('#tix_orders_list').append('<tr><td>'+bpt_order_time+'</td><td>'+bpt_date_id+'</td><td>'+bpt_quantity+'</td><td>'+bpt_fname+'</td><td>'+bpt_lname+'</td></tr>');
			}
		});
		
	});
	// Open the XML file
	$.get(xmlfile,{'func': 'tix'},function(xml){
	  	var total_tix = 0;
		var tickets_sold = $('bpt_tickets_sold',xml).text();
		jQuery('#post-178 .entry').append('<p id="bpt_tix_total">total: <span>'+tickets_sold+'</span></p>');
		// Run the function for each video tag in the XML file
		$('item',xml).each(function(i) {
			// need a variable for each possible option in the xml
			var date_id = $(this).find("bpt_date_id").text();
			var end_time = get_formatted_date(strtotime($(this).find("bpt_end_time").text()));
			var date_tickets_sold = $(this).find("bpt_date_tickets_sold").text();
			var price_collected_value = $(this).find("bpt_price_collected_value").text();
			//var cur_tix = $('#tix_total span').html();
//			cur_tix = cur_tix * 1;
//			date_tickets_sold = date_tickets_sold * 1;
//			var new_num = cur_tix+date_tickets_sold;
//			$('#bpt_tix_total span').html(new_num);
			if(date_id > 341692){
				jQuery('#post-178 .entry').append('<div class="bpt_tix_day" id="'+date_id+'"><span class="date">'+end_time+'</span> <span class="tix_sold">'+date_tickets_sold+'</span></div>');
				get_sold_list(date_id);
			}
		});
		
	});
	//jQuery('.bpt_tix_day .date').live('click',function(){
//		jQuery(this).find('.tix_orders_list').toggle();
//	});
	
});
function get_sold_list(date_id){
	// create new releases object array
	var xmlfile = 'http://anaphylaxistheplay.com/wordpress/wp-content/themes/baltimore-phototheme/bpt.php';
	
	// Open the XML file
	$.get(xmlfile,{'func': 'orders'},function(xml){
	  	jQuery('#'+date_id).append('<table border="0" cellpadding="0" cellspacing="0" width="100%" class="tix_orders_list"></table>');
		jQuery('#'+date_id+' .tix_orders_list').append('<tr><th>date ordered</th><th>date ID</th><th>num tix</th><th>First Name</th><th>Last Name</th></tr>');
		//jQuery('#tix_orders_list').after(xml);
		// Run the function for each video tag in the XML file
		$('item',xml).each(function(i) {
			// need a variable for each possible option in the xml
			bpt_order_time = get_formatted_date(strtotime($(this).find("bpt_order_time").text()));
			bpt_date_id = $(this).find("bpt_date_id").text();
			if(bpt_date_id == date_id){
				bpt_quantity = $(this).find("bpt_quantity").text();
				bpt_fname = $(this).find("bpt_fname").text();
				bpt_lname = $(this).find("bpt_lname").text();
				price_collected_value = $(this).find("bpt_price_collected_value").text();
				jQuery('#'+date_id+' .tix_orders_list').append('<tr><td>'+bpt_order_time+'</td><td>'+bpt_date_id+'</td><td>'+bpt_quantity+'</td><td>'+bpt_fname+'</td><td>'+bpt_lname+'</td></tr>');
			}
		});
		
	});
}
function get_formatted_date(unix_timestamp){
	// create a new javascript Date object based on the timestamp
// multiplied by 1000 so that the argument is in milliseconds, not seconds
var date = new Date(unix_timestamp*1000);
// hours part from the timestamp
var hours = date.getHours();
// minutes part from the timestamp
var minutes = date.getMinutes();
// seconds part from the timestamp
var seconds = date.getSeconds();
var this_date = date.getDate();
var day = date.getDay();
// minutes part from the timestamp
var month = date.getMonth();
// seconds part from the timestamp
var year = date.getFullYear();

// will display time in 10:30:23 format
var formattedTime = (month+1)+'/'+this_date+'/'+year+' '+hours + ':00';
return formattedTime;
}
function get_simple_date(unix_timestamp){
	// create a new javascript Date object based on the timestamp
// multiplied by 1000 so that the argument is in milliseconds, not seconds
var date = new Date(unix_timestamp*1000);
// hours part from the timestamp
var hours = date.getHours();
// minutes part from the timestamp
var minutes = date.getMinutes();
// seconds part from the timestamp
var seconds = date.getSeconds();
var this_date = date.getDate();
var day = date.getDay();
// minutes part from the timestamp
var month = date.getMonth();
// seconds part from the timestamp
var year = date.getFullYear();

// will display time in 10:30:23 format
var formattedTime = year+''+(month+1)+''+pad(this_date, 2);
return formattedTime;
}
function pad(number, length) {
   
    var str = '' + number;
    while (str.length < length) {
        str = '0' + str;
    }
   
    return str;

}

function strtotime(str, now) {
    // Convert string representation of date and time to a timestamp  
    // 
    // version: 902.2516
    // discuss at: http://phpjs.org/functions/strtotime
    // +   original by: Caio Ariede (http://caioariede.com)
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +      input by: David
    // +   improved by: Caio Ariede (http://caioariede.com)
    // %        note 1: Examples all have a fixed timestamp to prevent tests to fail because of variable time(zones)
    // *     example 1: strtotime('+1 day', 1129633200);
    // *     returns 1: 1129719600
    // *     example 2: strtotime('+1 week 2 days 4 hours 2 seconds', 1129633200);
    // *     returns 2: 1130425202
    // *     example 3: strtotime('last month', 1129633200);
    // *     returns 3: 1127041200
    // *     example 4: strtotime('2009-05-04 08:30:00');
    // *     returns 4: 1241418600
    var i, match, s, strTmp = '', parse = '';

    strTmp = str;
    strTmp = strTmp.replace(/\s{2,}|^\s|\s$/g, ' '); // unecessary spaces
    strTmp = strTmp.replace(/[\t\r\n]/g, ''); // unecessary chars

    if (strTmp == 'now') {
        return (new Date()).getTime();
    } else if (!isNaN(parse = Date.parse(strTmp))) {
        return parse/1000;
    } else if (now) {
        now = new Date(now);
    } else {
        now = new Date();
    }

    strTmp = strTmp.toLowerCase();

    var process = function (m) {
        var ago = (m[2] && m[2] == 'ago');
        var num = (num = m[0] == 'last' ? -1 : 1) * (ago ? -1 : 1);

        switch (m[0]) {
            case 'last':
            case 'next':
                switch (m[1].substring(0, 3)) {
                    case 'yea':
                        now.setFullYear(now.getFullYear() + num);
                        break;
                    case 'mon':
                        now.setMonth(now.getMonth() + num);
                        break;
                    case 'wee':
                        now.setDate(now.getDate() + (num * 7));
                        break;
                    case 'day':
                        now.setDate(now.getDate() + num);
                        break;
                    case 'hou':
                        now.setHours(now.getHours() + num);
                        break;
                    case 'min':
                        now.setMinutes(now.getMinutes() + num);
                        break;
                    case 'sec':
                        now.setSeconds(now.getSeconds() + num);
                        break;
                    default:
                        var day;
                        if (typeof (day = __is_day[m[1].substring(0, 3)]) != 'undefined') {
                            var diff = day - now.getDay();
                            if (diff == 0) {
                                diff = 7 * num;
                            } else if (diff > 0) {
                                if (m[0] == 'last') diff -= 7;
                            } else {
                                if (m[0] == 'next') diff += 7;
                            }

                            now.setDate(now.getDate() + diff);
                        }
                }

                break;

            default:
                if (/\d+/.test(m[0])) {
                    num *= parseInt(m[0]);

                    switch (m[1].substring(0, 3)) {
                        case 'yea':
                            now.setFullYear(now.getFullYear() + num);
                            break;
                        case 'mon':
                            now.setMonth(now.getMonth() + num);
                            break;
                        case 'wee':
                            now.setDate(now.getDate() + (num * 7));
                            break;
                        case 'day':
                            now.setDate(now.getDate() + num);
                            break;
                        case 'hou':
                            now.setHours(now.getHours() + num);
                            break;
                        case 'min':
                            now.setMinutes(now.getMinutes() + num);
                            break;
                        case 'sec':
                            now.setSeconds(now.getSeconds() + num);
                            break;
                    }
                } else {
                    return false;
                }

                break;
        }

        return true;
    }

    var __is =
    {
        day:
        {
            'sun': 0,
            'mon': 1,
            'tue': 2,
            'wed': 3,
            'thu': 4,
            'fri': 5,
            'sat': 6
        },
        mon:
        {
            'jan': 0,
            'feb': 1,
            'mar': 2,
            'apr': 3,
            'may': 4,
            'jun': 5,
            'jul': 6,
            'aug': 7,
            'sep': 8,
            'oct': 9,
            'nov': 10,
            'dec': 11
        }
    }

    match = strTmp.match(/^(\d{2,4}-\d{2}-\d{2})(\s\d{1,2}:\d{1,2}(:\d{1,2})?)?$/);

    if (match != null) {
        if (!match[2]) {
            match[2] = '00:00:00';
        } else if (!match[3]) {
            match[2] += ':00';
        }

        s = match[1].split(/-/g);

        for (i in __is.mon) {
            if (__is.mon[i] == s[1] - 1) {
                s[1] = i;
            }
        }

        return strtotime(s[2] + ' ' + s[1] + ' ' + s[0] + ' ' + match[2]);
    }

    var regex = '([+-]?\\d+\\s'
    + '(years?|months?|weeks?|days?|hours?|min|minutes?|sec|seconds?'
    + '|sun\.?|sunday|mon\.?|monday|tue\.?|tuesday|wed\.?|wednesday'
    + '|thu\.?|thursday|fri\.?|friday|sat\.?|saturday)'
    + '|(last|next)\\s'
    + '(years?|months?|weeks?|days?|hours?|min|minutes?|sec|seconds?'
    + '|sun\.?|sunday|mon\.?|monday|tue\.?|tuesday|wed\.?|wednesday'
    + '|thu\.?|thursday|fri\.?|friday|sat\.?|saturday))'
    + '(\\sago)?';

    match = strTmp.match(new RegExp(regex, 'g'));

    if (match == null) {
        return false;
    }

    for (i in match) {
        if (!process(match[i].split(' '))) {
            return false;
        }
    }

    return (now);
}
