//Navigation Artists Dropdown on hover
function navOver() {
	$("#main_nav li.dropdown").hover(function() {
		$("#dropdown").show();
	}, function() {
		$("#dropdown").hide();
	});
}

//Loading Homepage Flash
function loadHomeFlash() {
	var flashvars = { xmlUrl: "/swf/cami_music_xml.asp", swfUrl: "/swf/cami_music.swf", exportType: "browser" };
	var params = { wmode: "transparent" };
	var attributes = {};
	swfobject.embedSWF("/swf/cami_music_main.swf", "flash_content", "935", "652", "9.0.0", "/swf/expressInstall.swf", flashvars, params, attributes);
}

//Modal Popup Effects
var pageName;
var downloadPath; //To store thumbnail image path
$(function() {
	if($("#store").length) {
		$("#dialog").dialog({
			bgiframe: true,
			autoOpen: false,
			Height: 567,
			width: 616,
			modal: true,
			close: function() {
				restoreLinks();
			}
			//closePopup function called on the product page for store
		});
	} else {
		$("#dialog").dialog({
			bgiframe: true,
			autoOpen: false,
			height: 567,
			width: 616,
			modal: true,
			open: function() {
				closePopup();
			},
			close: function() {
				restoreLinks();
			},
			buttons: {
				"I Agree...": function() {
					$(this).dialog('close');
					downloadPhoto();
				},
				"Cancel": function() {
					$(this).dialog('close');
				}
			}
		});
        $("#audio_dialog").dialog({
			bgiframe: true,
			autoOpen: false,
			height: 567,
			width: 616,
			modal: true,
			open: function() {
				closePopup();
			},
			close: function() {
				restoreLinks();
			},
			buttons: {
				"I Agree...": function() {
					$(this).dialog('close');
				},
				"Cancel": function() {
					$(this).dialog('close');
				}
			}
		});

	}
});
function restoreLinks() {
	$("#store a.on").attr("href", pageName);
	$("#store a.on").removeClass();
	$("#gallery a.on").attr("href", pageName);
	$("#gallery a.on").removeClass();
}
function closePopup() {
	$("div.ui-widget-overlay").click(function() {
		$('#dialog').dialog('close');
	});
	$("a.close").click(function() {
		$('#dialog').dialog('close');
	});
}
function openPopup(elementName) {
	//pageName = $(elementName).attr("href");
	if($("#store").length) {
		pageName = $(elementName).attr("href");
	} else {
		pageName = "/artists/download-release.html";
	}
	$(elementName).attr("href", "javascript:;");
	$(elementName).addClass("on");
	$("#dialog").load(pageName);
	$('#dialog').dialog('open');
}
function showPopup() {
	$("#store a").click(function() {
		openPopup(this);
	});
}
function audioTermsPopup() {
    $("#music_terms").click(function() {
        $("#audio_dialog").load("/artists/download-audio-release.html");
	    $('#audio_dialog').dialog('open');
    });
}

//to change cancel button styles
function addBtnStyles() {
	$("div.ui-dialog-buttonpane button:eq(1)").addClass("cancel");
}

//Artist Page Video Player
function loadVideo(startMode) {
	var filepath = "/flv/" + $("#artist_video").attr("name");
	var flashvars = { currentVideo: filepath, autoStart: startMode };
	var params = { allowFullScreen: true, menu: true, wmode: "transparent" };
	var attributes = {};
	swfobject.embedSWF("/swf/video_player.swf", "artist_video", "312", "234", "9.0.0", "/swf/expressInstall.swf", flashvars, params, attributes);
}
function switchVideo() {
	$("#playlist li a").click(function() {
		var storeVideo = $(this).attr("href");
		var videoTitle = $(this).html();
		$("#sidebar h3").eq(0).html(videoTitle);
		$("#artist_video").attr("name", storeVideo);
		loadVideo(true);
		return false;
	});
}

//Artist Page Photo Gallery
function photoGallery() {
	$("#handle").slider({
		orientation: "vertical",
		max: 100,
		min: 0,
		value: 100,
		slide: function(event, ui) {
			$("#gallery ul").animate({'top': (100 - ui.value) * -0.01 * ($("#gallery ul").outerHeight() - 242)}, 0);
		}
	});
	$("#slider").click(function() {
		$("#gallery ul").animate({'top': "0%"}, 500);
		$("#handle a").animate({"bottom": "100%"}, 500);
	});
}
function popupPhoto() {
	$("#gallery li a").click(function() {
		openPopup(this);
		downloadPath = this; //global variable set to store current thumbnail clicked
	});
}
function downloadPhoto() {
	var imagePath = $(downloadPath).find("img").attr("src");
	var url = imagePath.replace("thumbs", "hi_res_photos");
	window.open(url, "imgwin1", "height=600,width=500,top=0,left=0,menubar=no,toolbar=no,location=0,status=0,resizable=1,scrollbars=1").focus();
	//imgwin1.window.status="Image you requested";
}

//Contact Form Validation
function formValidate() {
	$("#form_fields").validate({
		rules: {
			fullname: "required",
			email: {
				required: true,
				email: true
			},
			comment: "required"
		},
		messages: {
			fullname: "*",
			email: {
				required: "*",
				email: "*"
			},
			comment: "*"
		},
		errorPlacement: function(error, element) {
			error.insertBefore(element);
		},
		invalidHandler: function() {
			$("#errormsg").show();
			$("#thanksmsg").hide();
		}
	});
}

//Function calls
$(document).ready(function() {
	navOver();
	if($("#home_flash").length) {
		loadHomeFlash();
	}
	if($("#store").length) {
		showPopup();
	}
	if($("#sidebar").length) {
		photoGallery();
		popupPhoto();
		loadVideo(false);
		switchVideo();
        audioTermsPopup();
	}
	if($("#contact_form").length) {
		formValidate();
	}
});

