var siteURL = "http://www.wickerparkbucktown.com";
var siteSecureURL = "https://wpbcc.worldsecuresystems.com";

/*
-- Description: Initiates the selectbox plugin
*/
jQuery(function() {
    jQuery(".search_drop").selectbox();
});

/*
-- Description: Loads the nivo slide if an element with id "slider" exists
*/
jQuery(window).load(function() {
	if (jQuery('#slider').html())
	{
    jQuery('#slider').nivoSlider({
        effect: 'fade',
        // Specify sets like: 'fold,fade,sliceDown'
        animSpeed: 500,
        // Slide transition speed
        pauseTime: 5000,
        // How long each slide will show
        startSlide: 0,
        // Set starting Slide (0 index)
        directionNav: false,
        // Next & Prev navigation
        controlNav: true,
        // 1,2,3... navigation
        keyboardNav: false,
        // Use left & right arrows
        captionOpacity: 1,
        // Universal caption opacity
        pauseOnHover: false // Stop animation while hovering
    });
	}
});

/*
-- Description: On The Home Page makes the box layout have equal margins and line up correctly
*/
jQuery(function() {
    jQuery("#box3").css("margin", "0px");
});

/*
-- Description: This function checks to see if the user is editing the web app and if so, change the style of the page
*/
jQuery(function() {
    if (document.location.href.indexOf('?A=Edit') > -1) {
        jQuery("#sr-two-col-right").hide();
        jQuery("#sr-three-col-middle").css("width", "690px");
    }
});


/*
-- Description: Initiates the facnybox plugin for all "inline" classes contained within an a tag
*/
jQuery(document).ready(function() {
    jQuery("a.inline").fancybox({
        'hideOnContentClick': true
    });
	 jQuery("a.single_image").fancybox();
	 
	 jQuery(".form").fancybox({
		'padding'			:10,
		'margin'			:10,
		'width'				: 680,
		'height'			: 500,
        'autoScale'     	: false,
        'transitionIn'		: 'none',
		'transitionOut'		: 'none',
		'scrolling'			: 'no',
		'type'				: 'iframe'
	});

});

/*
FUNCTION setDefaultDomain
-- Inputs: None
-- Outputs: None
-- Author: Chicago Digital
-- Description: This function searches for all links contained within the a tag and replaces the domain name with the default domain and sets the protocol to HTTP://
*/
function setDefaultDomain()
{
	var links = document.getElementsByTagName('A');

	for (i = 0; i < links.length; i++)
	{
		if (links[i].getAttribute('href'))
		{
			var href = links[i].getAttribute('href');
			if (href.substring(0,4) != 'http' && href.substring(0,1) == '/')
			{
				//this example requires your links to have absolute paths.
				//document-relative paths and absolute URLs are not supported.
				links[i].setAttribute('href', siteURL + href);
			} 
		}
	}
}

/*
FUNCTION switchURLToSecure
-- Inputs: None
-- Outputs: None
-- Author: Chicago Digital
-- Description: This function checks to see if the user is accessing the website from and non secure protocol and if so, redirects them to the secure version of the page
*/
function switchURLToSecure()
{
var sitePath = window.location.pathname;
if (window.location.protocol == "http:") window.location = siteSecureURL+sitePath;
}

/*
FUNCTION switchURLToDefault
-- Inputs: None
-- Outputs: None
-- Author: Chicago Digital
-- Description: This function checks to see if the user is accessing the website from a secure protocol and if so, redirects them to the non-secure default version of the page
*/
function switchURLToDefault()
{
var sitePath = window.location.pathname;
if (window.location.protocol == "https:") window.location = siteURL+sitePath;
}

/*
FUNCTION replaceNoItemsFound
-- Inputs: 
			- elementReference: This is the class, id, or other object that this function will operate on
			- replaceText: This is the text that will replace the No Items Found Message
-- Outputs: None
-- Author: Chicago Digital
-- Description: This function checks to see if the elementRefernce contains "No Items Found." and if it does, it will be replaced by the replaceText parameter
*/
function replaceNoItemsFound(elementReference, replaceText, replacePad)
{
var insideText = $(elementReference).html(); 
var noItems = insideText.search("No items found.");
if (noItems != "-1") 
{
	$(elementReference).html(replaceText);
	if (replacePad) $(elementReference).css("padding-left", replacePad);
}

}

/*
FUNCTION replaceNoItemsFound
-- Inputs: 
			- elementReference: This is the class, id, or other object that this function will operate on
			- imageReference: This is the class, id, or other object where the image is contained
			- defaultImageReference: This is the class, id, or other object where the default image is contained
-- Outputs: None
-- Author: Chicago Digital
-- Description: This function checks to see if an image has been added and if not, sets a default image.
*/
function swapDefaultImage(elementReference, imageReference, defaultImageReference)
{
        if($(elementReference).attr("src") == null || $(elementReference).attr("src").search("Action=thumbnail") == 1)
         {
             $(imageReference).hide();
             $(defaultImageReference).show();
          }
}

/*
FUNCTION mustComeFrom
-- Inputs: 
			- referralURL: The page the user must come from and will be redirected to if the did not
-- Outputs: None
-- Author: Chicago Digital
-- Description: This function checks to see if a user came from a certain page and if they did not, it will redirect the user to that page.
*/
function mustComeFrom(referralURL)
{
// Make sure the user came from the referralURL
referralPage = document.referrer;
if (referralPage.search(referralURL) == -1) location.href = referralURL;
}

/*
FUNCTION changeOnEditForm
-- Inputs: 
			- originalElement: This is the element that is currently showing that will be switched to hidden.
			- newElement: This is the element that is currenlty hiden that will be switched to visisble
-- Outputs: None
-- Author: Chicago Digital
-- Description: This function checks to see if a user is on a Web App edit form and if so it switches the visibility of the elements.
*/
function changeOnEditForm(originalElement, newElement)
{
if (document.URL.search("Edit") != -1)
{
	$(originalElement).hide();
	$(newElement).show();
}
}

/*
FUNCTION writeBox
-- Inputs: 
			- bImage: The image to be used
			- bLink: The link to be used
			- bHeadline: The text for the main headline
			- bLine1: The text for the first line
			- bLine2: The text for the second line
-- Outputs: None
-- Author: Chicago Digital
-- Description: This function creates a boxed style for a set of conetent that is passed into it.
*/
function writeBox(bImage, bLink, bHeadline, bLine1, bLine2)
{
	document.write("<div class='box_list'>");
	document.write("<div class='box_list_img'><a href='"+bLink+"'><img alt='' src='"+bImage+"?Action=thumbnail&amp;Width=181&amp;Height=132&amp;algorithm=fill_proportional' width='181' height='132' /></a></div>");
	document.write("<div class='box_list_info'>");
	document.write("<span class='box_bus_name'>"+bHeadline+"</span>");
	document.write("<span class='box_offer'>"+bLine1+"</span>");
	document.write("<span class='box_exp'>"+bLine2+"</span>");
	document.write("</div>");
	document.write("<a class='box_more' href='"+bLink+"'></a></div>");
}

/*
FUNCTION convertDateFromBC
-- Inputs: 
			- bcDate: A date in BC Format ("11-Jan-2012")
-- Outputs: 
			- javaDate: A date in javascript format
-- Author: Chicago Digital
-- Description: This function converts a date from BC to javascript format
*/

function convertDateFromBC(bcDate)
{
var dateSeperated = bcDate.split('-');
var monthname=["Jan","Feb","Mar","Apr","May","Jun", "Jul","Aug","Sep","Oct","Nov","Dec"];
var javaDate = new Date(dateSeperated[2], monthname.indexOf(dateSeperated[1]), dateSeperated[0], 0, 0, 0, 0); 
return(javaDate);
}
