$(function(){
	
	// init search field
	
	$(".toggle").toggleVal();

	init_roundcorners();
	
	init_magnify_videos();
	
	init_colorbox();				
	
});




function enable_fun_stuff() {
	
	var is_ie  		= $("body").hasClass("ie");
	var is_cms 		= $("body").hasClass("cms");
	var is_chrome 	= $("body").hasClass("chrome");
		
	return (!is_ie && !is_cms && !is_chrome);
	
}



function init_magnify_videos() {
	
	// if (typeof(cms)!="undefined") { return false; }
	
	$("a.magnify_video").click(function(e){
		
		// stop the default action of the 
		// link from working
		
		e.preventDefault();
		
		// split the HREF by / and seperate 
		// the video code from the rest
		// of the URL.  
				
		var href = $(this).attr("href");
				
		var href_split = href.split("\/");
		
		var video_code = href_split[href_split.length - 1];
		
		
		// now build the iframe 
				
		var iframe = $('<iframe></iframe>').attr({
						  src: 			'http://video.personaldefensenetwork.com/video/'+video_code+'/player?layout=',
						  width: 		'420',
						  height: 		'451',
						  scrolling: 	'no',
						  frameborder: 	'0'		  
						});
											

		// hide the original, place the 
		// new iframe before it, then
		// remove the original
											
		$(this).hide();
		
		iframe.insertBefore(this).show();
		
		$(this).remove();	
		
		// return false to secondarily
		// prevent the link from it's
		// default action
		
		return false;	
		
	});
	
}



function init_roundcorners() {
	
	if (!enable_fun_stuff()) { return false; }
	
	if (!jQuery.fn.corners) { return false; }
	
	$("a.round_button").corners("12px");
			
}



function init_colorbox() {
	
	if (!jQuery.fn.colorbox) { return false; }
		
	$("a.colorbox, a.lightbox")
		.colorbox({	opacity: .5,
					speed: 500, 
					maxWidth: "75%", 
					maxHeight: "90%",
					onOpen: function() {
						
						//$(this).attr("title",$(this).attr("caption"));						
						
					}});
	
}


function init_homepage_cycle() {
	
	if (!jQuery.fn.cycle) { return false; }

	// init the cycle differently if 
	// user is logged into cms.
	
	if (typeof(cms)=="undefined") {		
			
		$("#carousel").cycle({
			
			fx:			'fade',			
			easing:		'swing',
			pause:		true,
			speed:		1000,
			timeout:	7000,
			delay: 		-2000,
			fastOnEvent:750,
			pager:		'#carousel-pager'
			
		});
			
	} else {
		
		// the cms requires some more
		// barebones stuff for the cycle.
		// no transition effects, no 
		// automatic cycling, and the
		// pager needs to ask the user
		// if they want to save before 
		// cycling.
		
		$("#carousel").cycle({
			
			fx:			'none',			
			easing:		'swing',
			timeout:	0,
			pager:		'#carousel-pager',
			pagerClick:	function(index,elem){
									
				$("div.hoverBox").hide();
				
				if (!editor.save_before_leaving(function(){
					
					$("#carousel div").not(elem).hide();
					
				})) { editor.hide(); }
				
				$("#carousel div.hide").removeClass("hide");
				
			}
			
		});
		
	}
		
}



function init_rating_stars(page_id) {
	
	if (!jQuery.fn.stars) { return false; }	
	
	$("#rating-form").data("page_id",page_id)
					 .stars({	captionEl: $("#rating-caption"),
								oneVoteOnly: true,			
								callback: rating_stars_save	});
	
}


function rating_stars_save(ui, type, value) {
			
	var script = absPath+"includes/ratings/";
	
	var page_id = $("#rating-form").data("page_id");
	
	var params = {action: "save", id: page_id,score: value};
	
	$("#rating-caption").html("Saving...");
	
	$.getJSON(script,params,function(json) {
		
		if (json.error) {
			
			$("#rating-caption").html("Error While Saving").css("color","red");
			
		} else {
		
			//var rounded = Math.round(json.msg.average);
			//$("#rating-form").stars("select", rounded);	
			
			$("#rating-caption").html("Your Rating Saved: "+ui.options.title+"");	
			$("#rating-average").html(json.msg.average+" ("+json.msg.votes+" Votes)");
			
		}
		
		
	});	
	
	
}