/* 
 * This file accompanies ajax.php
 * This is the sites frontend logic for jQuery/AJAX Calls to ajax.php
 */
//if(!inc) var inc = "include/ajax/";

/* Sidebar Functions */

function SIDE_UpdateModelField() {
	var make = $("#side_make").val();
	$.get(inc+"ajax.php", {func: "SIDE_UpdateModelField", make: make}, function(data) {
		$("#side_model").html(data);
	});
}

/* Blog Functions */
function BLOG_Get(pages, content, page) {
	// Manifest of functions
    BLOG_GetPosts(content, page);
    BLOG_GetPages(pages, content, page);

    activepage = page;
    
}
function BLOG_GetTag(pages, content, page, tag) {
	$.get(inc+"ajax.php", {func: "BLOG_GetPosts", page: page, tag: tag}, function(data) {
		$(content).html(data);
	});
	//tag = "&tag="+tag; /////////////////////////////////// :::::TODO:::: Finish Tags Search
	BLOG_GetPages(pages, content, page, tag);
	
	activepage = page;
}

function BLOG_GetPosts(field, page) {
    if(!page || page == "") page = 1;
    $.get(inc + "ajax.php", {func: "BLOG_GetPosts", page: page}, function(data) {
        $(field).html(data);
    });
    
}
function BLOG_GetPages(pages, content, page, tag) {
	if(!tag || tag == "") tag = "";
	if(!page || page == "") page = 1;
	

    $.get(inc + "ajax.php", {func: "BLOG_GetPages", page: page, content: content, tag: tag}, function(data) {
        $(pages).html(data);
    });
}

// a "PageRocker" is the "Prev" and "Next" buttons found on the pagination system
// BLOG and INVentory are separated based on functional needs. Find the INV one at the bottom of Inventory functions (below)
function BLOG_PageRocker(pages, content, direction, tag) {
    if(!tag || tag == "") tag = "";
    apage = activepage;

    if(direction == "next") apage++;
    else if(direction == "prev") apage--;

    if((apage < 1) || (apage > totalpages)) {}
    else {
		BLOG_SwitchPage(apage, pages, content, tag);
        
    }

}

function BLOG_SwitchPage(newpage, pages, content, tag) {
	window.location.hash = newpage;
	if(!tag) BLOG_Get(pages, content, apage);
	else BLOG_GetTag(pages, content, apage, tag);
}

/* Inventory Functions */
function INV_Get(page, sort, cond) {
	if(!sort || sort == "") sort = "";
	if(!cond || cond == "") cond = "";
	// Manifest of functions
	INV_GetInv(page, sort, cond);
	INV_GetPages(page, cond);
	
	activepage = page;
}
function INV_GetInv(page, sort, cond) {
	if(!sort || sort == "") sort = "";
	if(!cond || cond == "") cond = "";
	if(!page || page == "") page = 1;
	// Override hack, if sorting by New or Used
	if(sort != "") {
		if(sort == 'new' || sort == 'used') cond += "&orderby=year&cond="+sort;
		else cond += "&orderby="+sort;
	}
	$.ajax({
		type: "GET",
		url: inc+"ajax.php?func=INV_GetInv&page="+page+cond,
		success: function(data) {
			$("#inventory").html(data);
		}
	});
}

function INV_GetPages(page, cond) {
	if(!page || page == "") page = 1;
	/*
	$.get(inc + "ajax.php", {func: "INV_GetPages", page: page}, function(data) {
		$(".paging").html(data);
	});
	*/
	$.ajax({
		type: "GET",
		url: inc+"ajax.php?func=INV_GetPages&page="+page+cond,
		success: function(data) {
			$(".paging").html(data);
		}
	});

}

function INV_PageRocker(direction) {
	apage = activepage;
	
	if(direction == "next") apage++;
	else if (direction == "prev") apage--;
	
	// Debug
	//alert("Direction: " + direction + "\nActivepage: " + activepage + "\napage: " + apage);
	// End Debug
	
	if((apage < 1) || (apage > totalpages)) {}
	else
		INV_SwitchPage(apage);
}

function INV_SubmitOffer() {
	$.post(inc + "ajax.php?func=INV_SubmitOffer", {
		name: $("#name").val(),
		email: $("#email").val(),
		phone: $("#phone").val(),
		stock: $("#stock").val(),
		offer: $("#offer").val(),
		message: $("$message").val()
	}, function(data) {
		if(data == "success") {
			$("#make-an-offer-form").html("You offer has been submitted. <br /> Thank you!");
			$("#make-an-offer-form").fadeOut('slow');
		} else {
			$("#make-an-offer-form").html("There was an error submitting your offer. <br /> Please try again.");
		}
	});
}


function INV_SendToFriend() {
	$.post(inc + "ajax.php?func=INV_SendToFriend", {
                friend_name: $("#stf-friend-name").val(),
		friend_email: $("#stf-friend-email").val(),
		your_email: $("#stf-my-email").val(),
		your_name: $("#stf-my-name").val(),
		message: $("#stf-message").val(),
                url: "http://kelownarv.nav/rv-inventory-details.php?id="+$("#stf-rv-id").val()
	}, function (data) {
		if(data == "success") {
                    $("#send-to-friend-form").html("Mail sent!<br /> Thank you!");
                    $("#send-to-friend-form").fadeOut('slow');
		} else {
                    $("#send-to-friend-form").html("There was an error sending your email.<br /> Please try again.");
		}
	});
}

