//generic modal open function
// urlToLoad param is optional
function ctcmodal_open (event, modal_wrapper, bindFn, urlToLoad){
	var $e = $(event.target);
	var href = $e.attr('href');
	//clear contents of placeholder if necessary
	if(modal_wrapper.html().length > 0){
		modal_wrapper.html('');
	}
	//testing for optional function param
	if (urlToLoad == null || urlToLoad == ''){
		//if nothing passed as param looks to href attribute of target
		urlToLoad = href;
	}
	//if we have a path to load modal contents from go and get it
	if (urlToLoad != undefined && urlToLoad != null) {
		modal_wrapper.load(urlToLoad, function(){
			//bind events for modal contents
			bindFn(modal_wrapper);
			//show modal
			modal_wrapper.dialog('open');
		});
		
	}
}

/********** START :: Click to call specific ***************/

$().ready(function(){
	init_CTC_modals();
});

var CTC_modal_opts = {
	bgiframe: true,
	modal: true,
	autoOpen: false,
	draggable: false,
	resizable: false,
	position: 'center',
	zIndex: 1000,
	width: 732
};

function init_CTC_modals(){
	//this dynamically finds links with a specific class and sets up modal placeholder if they exist
	// also binds click event that uses href attribute to load content
	var CTC_modal_links = $('a.CTC_modal');
	//if there are any on page
	if (CTC_modal_links.length > 0) {
		var CTCph = $('#CTC_ph');
		if (CTCph.length != 1) {
			$('#wrapper').append('<div id="CTC_ph"></div>');
			CTCph = $('#CTC_ph');
		}
		//initialize modal - this happens only once per placeholder
		CTCph.dialog(CTC_modal_opts);
		//bind click events to links to open modal
		CTC_modal_links.click(function(event){
			//prevent default stops browser default action - on links this means navigating to url or jumping to top of page on # hrefs
			event.preventDefault();
			ctcmodal_open(event, CTCph, bind_CTC_modal_evts);
		});
	}
}

//bind your interactive events here (close, purchase, view and color rollovers)
function bind_CTC_modal_evts(modal_wrapper){
	//close
	modal_wrapper.find('.close').click(function(event){
		//prevent default stops browser default action - on links this means navigating to url or jumping to top of page on # hrefs
		event.preventDefault();
		clearError();
		modal_wrapper.dialog('close');
		modal_wrapper.html('');
	});
}
