var $j = jQuery.noConflict();


function setFieldValue(myFieldID, myFieldValue){
	//alert('set field: '+myFieldID+', '+myFieldValue);
	document.getElementById(myFieldID).value = myFieldValue;
	$j('input[rel=security_code_check]').val(myFieldValue);
}

// //////////////////////////
// http://adipalaz.awardspace.com/experiments/jquery/accordion2.html
// /////////////////////////
(function($j) {
	$j.fn.orphans = function() {
		var txt = [];
		this.each(function() {
			$j.each(this.childNodes, function() {
				if (this.nodeType == 3 && $j.trim(this.nodeValue)){
					txt.push(this);
				}
			});
		});
		return $j(txt);
	};
	// http://www.learningjquery.com/2008/02/simple-effects-plugins:
	$j.fn.slideFadeToggle = function(speed, easing, callback) {
		return this.animate( {
			opacity : 'toggle',
			height : 'toggle'
		}, speed, easing, callback);
	};
})(jQuery);
// ////////////////////////////

$j(function() {
	$j('.parent').css('cursor', 'pointer').orphans().wrap(
			'<a href="#" onclick="return false;" title=""></a>');
	//$j('ul').find('ul.collapse:gt(0)').hide();
	

});

function doToggleChild(childID){
		parentID = '#parent_'+childID.split('_')[1];
		$j('#'+childID).slideToggle().end();
		$j(parentID).toggleClass('active').siblings().removeClass('active').end();
		allChildren = $j('.child').each(function(){
					 checkID = $j(this).attr('id');
						 if(checkID != childID){
							$j('#'+checkID).slideUp().end();
						 }
					}
				);

}


function initNav(){
    $j('.no_subnav').each(function(){
            $j(this).click(
                function(event){
                allChildren = $j('.child').each(
                    function(){
                    checkID = $j(this).attr('id');
                    $j('#'+checkID).slideUp().end();
                    $j('.child_li').each(function(){				   
                        $j(this).removeClass('active');
                    });
                });
            });
        });

    $j('.child_li').each(function(){
            $j(this).click(
                function(event){
                $j(this).addClass('active').siblings().removeClass('active active_last').end();
                });
            });
}

function setActiveNav(path){
	path = path.split('/page')[0];
	var activeNavURL = path;
	if(path.indexOf('/voices') > -1){
		checkIsMediaDetail(path);
		return;
	}
	if(path == '#'){
		return;
	}
	
	if(!path in $j.myNavURLs){
		return;
	}
	isValidNavURL = false;
	for(i=0; i<$j.myNavURLs.length; i++){
		if($j.myNavURLs[i] == path){
			isValidNavURL = true;
			break;
		}
	}
	
	if(!isValidNavURL){
		checkIsMediaDetail(path);
		return;
	}
	
	$j('.ajax_nav_link').each(function(){
            url = $j(this).attr('href');
            path = path.split('/index/page:')[0];
            if(path == url){
            var foundMatch = true;
            resetBreadcrumbs(path, $j(this).html());
            $j(this).siblings().removeClass('active');
            $j(this).addClass('active');
            $j(this).parent().addClass('active').siblings().removeClass('active');
            if($j(this).parent().parent().hasClass('child')){
            //alert($j(this).parent().parent().attr('id'));
            $j(this).parent().parent().parent().addClass('active').siblings().removeClass('active');
            $j(this).parent().parent().slideDown();
            curOpen = $j(this).parent().parent().parent().attr('id');
            var curChildID = 'child_'+curOpen.split('_')[1];
            $j('.child').each(
                function(){
                    if($j(this).attr('id') != curChildID){
                        $j(this).slideUp();
                    }
                });
            }
            else if($j(this).parent().hasClass('no_subnav')){
                $j('.child').each(function(){
                        $j(this).slideUp();
                        });
            }
            }
            else{
                $j(this).parent().removeClass('active');
                $j(this).parent().addClass('inactive');
            }
    }
    );
}

function checkIsMediaDetail(path){
	checkString = '';

	if(path.indexOf('/photos') > -1){
		checkPath = ['/photos'];
		checkString = ['Photos'];
	}
	if(path.indexOf('/videos') > -1){
		checkPath = ['/videos'];
		checkString = ['Videos'];
	}
	if(path.indexOf('/voices') > -1){
		checkPath = ['/voices'];
		checkString = ['Voices'];
		if(typeof($j.voiceTypeName) != 'undefined' && $j.voiceTypeName != ''){
			checkString.push($j.voiceTypeName);
			//$j.voiceTypeName = '';
		}
		else{
			checkString.push('All');
		}
	}
	
	if(path.indexOf('/works/view') > -1 || path.indexOf('/recordings/view') > -1){
		checkPath = ['/works', '/performances', '/albums/view', '/recordings/view'];
		checkString = ['Music', 'Works', 'Compositions', 'Performances', 'Tracks'];
	}
	
	if(checkString == ''){
		return;
	}
	
	$j('.ajax_nav_link').each(function(){
									  label = $j(this).html();
									  url = $j(this).attr('href');
									  for(var l=0; l<checkString.length; l++){
										  if(label == checkString[l]){
											for(var i=0; i<checkPath.length; i++){
												if(url.indexOf(checkPath[i]) > -1){
													//alert(checkPath[i] +' : '+ checkString[l]);
													var foundMatch = true;
														resetBreadcrumbs(path, $j(this).html());
														$j(this).addClass('active').siblings().removeClass('active').end();
														$j(this).parent().addClass('active').siblings().removeClass('active').end();
														if($j(this).parent().parent().hasClass('child')){
															//alert($j(this).parent().parent().attr('id'));
															$j(this).parent().parent().parent().addClass('active').siblings().removeClass('active').end();
															$j(this).parent().parent().slideDown();
															curOpen = $j(this).parent().parent().parent().attr('id');
															var curChildID = 'child_'+curOpen.split('_')[1];
															$j('.child').each(function(){
																				if($j(this).attr('id') != curChildID){
																					$j(this).slideUp();
																				}
																			});
														}
														else if($j(this).parent().hasClass('no_subnav')){
															$j('.child').each(function(){
																				$j(this).slideUp();
																				});
														}
													}
												}
										  }
										  else{
											//$j(this).parent().removeClass('active');
											$j(this).parent().addClass('inactive');
										  }
									  }
								  }
							);
	
}

function initNavURLArray(){
	$j.myNavURLs = new Array();
	
	$j('.ajax_nav_link').each(function(){
		$j.myNavURLs.push($j(this).attr('href'));
	});
}


function initClearCartPrompt(){
	if(typeof(showClearCartPrompt) != 'undefined'){
		promptToClearCart();
	}
}

function adjustBodyImagePadding(){
	$j('.body_copy img, .body_copy table').each(function(){
									   	align = $j(this).attr('align').toLowerCase();
										if(align == 'left'){
											$j(this).css({'margin-left':'0px'});
										}
										if(align == 'right'){
											$j(this).css({'margin-right':'0px'});
										}
									   });
	
	$j('.body_copy table img').each(function(){
										$j(this).css({'margin-left':'0px'});
										$j(this).css({'margin-right':'0px'});
									   });
}

function checkForPathOverride(){
    u = window.location.href.split("#");
    if(u.length != 1){ $j(".ajax_nav_link[href="+u[1]+"]").trigger('click'); }
}

$j(document).ready(function(){
	initNav();
	initNavURLArray();
	initClearCartPrompt();
	adjustBodyImagePadding();
    checkForPathOverride();
});
$j(window).ready(function(){
});

