//---------------------------------------------------------------------------------------------------------------------------------------------
// when the DOM is ready...
//---------------------------------------------------------------------------------------------------------------------------------------------
$(document).ready(function () {

	var last_add = "";
	var last_click = '';
	
    var $panels = $('#slider .scrollContainer > div');
    var $container = $('#slider .scrollContainer');

    // if false, we'll float all the panels left and fix the width 
    // of the container
    var horizontal = true;

    // float the panels left if we're going horizontal
    if (horizontal) {
        $panels.css({
            'float' : 'left',
            'position' : 'relative' // IE fix to ensure overflow is hidden
        });

        // calculate a new width for the container (so it holds all panels)
        $container.css('width', $panels[0].offsetWidth * $panels.length);
    }

    // collect the scroll object, at the same time apply the hidden overflow
    // to remove the default scrollbars that will appear
    var $scroll = $('#slider .scroll').css('overflow', 'hidden');
	
    // handle nav selection
    function selectNav() {
    	
    	//bread crumbs
    	var my_title = '';
    	my_title = String($(this).attr('title'));
    	
    	if(my_title != '') {
			if (my_title != 'undefined'){
				if(my_title != last_add){
					//add to breadcrumbs
					var my_href = $(this).attr('href');
					$("#breadcrumbs").append('<li>/</li>');
					$("#breadcrumbs").append('<li><a href="'+ my_href +'">'+my_title+'</a></li>');
					
					last_add = my_title;
					
					 $('#breadcrumbs').find('a').unbind('click');
					 $('#breadcrumbs').find('a').bind('click', function(e){ 
						$('.pg_data').remove();
					 	$(this).parent().nextAll().remove();
					 	last_add = ''; //$(this).parent();
					 });
				 }
			}
		}
    	
    	
        $(this)
            .parents('ul:first').find('a').removeClass('selected').end()
            .end()
            .addClass('selected');
    }

    $('#slider .navigation').find('a').bind('click', function(){selectNav()} );

    // go find the navigation link that has this target and select the nav
    function trigger(data) {
    	
    	var my_eid = String(Number($(data).attr('relid')));
    	
    	if(my_eid != 'NaN'){
    		
    		//------------------------------
    		var e_name = $('h3', data).text();
    		var e_id = $(data).attr('relid');
    		
			var requestUrl = "./spots.php";
			requestUrl += "?editor="+e_name+"&credit="+e_id;
			requestUrl += "&sid="+Math.random();
			
			$('.pg_data').remove();
			
			
			$.get(requestUrl, {eid: e_id, editor:e_name}, function(req_data){
				$(data).append('<div class="pg_data"></div>');
			
				$(".pg_data").html(req_data);
				$("<div class='vid_content' id='vid_content'></div>").appendTo(".pg_data");
				
				var id_array = location.hash.slice(1).split('/');
				if(id_array.length == 3){
					var find_str = "a[vid=" + id_array[2] + "]";
					var el = $(find_str).get(0);
					$(el).click();
				}
				
				getReels(e_id, e_name);
			});
    	} else {
    		$('.vid_content').html('');
    		$('#vid_content').html("");
    	}
    	$('#vid_content').html("");
        var el = $('#slider .navigation').find('a[href$="' + data.id + '"]').get(0);
        selectNav.call(el);
    }

    if (window.location.hash) {
    	var urlstr = window.location.hash.substr(1);
    	var url_array = urlstr.split("/");
    	var panelid = url_array[0];
    	
    	var el = $('#slider .navigation').find('a[href$="#editor"]').get(0);
        selectNav.call(el);
        
        //var el = $('#slider .navigation').find('a[href$="#paul-kelly"]').get(0);
        //selectNav.call(el);
        
        //trigger({ id : panelid });
        
    } else {
       // $('ul.navigation a:first').click();
    }

    // offset is used to move to *exactly* the right place, since I'm using
    // padding on my example, I need to subtract the amount of padding to
    // the offset.  Try removing this to get a good idea of the effect
    var offset = parseInt((horizontal ? 
        $container.css('paddingTop') : 
        $container.css('paddingLeft')) 
        || 0) * -1;


    var scrollOptions = {
        target: $scroll, // the element that has the overflow

        // can be a selector which will be relative to the target
        items: $panels,

        navigation: '.navigation a',

        // selectors are NOT relative to document, i.e. make sure they're unique
        prev: 'img.left', 
        next: 'img.right',

        axis: 'xy',			// allow the scroll effect to run both directions
        onAfter: trigger, 	// our final callback
        offset: offset,
        duration: 300,  	// duration of the sliding effect

        // easing - can be used with the easing plugin:  // http://gsgd.co.uk/sandbox/jquery/easing/
        easing: 'swing'
    };

    // apply serialScroll to the slider - it supports the indexed next and previous scroll along with hooking in to our navigation.
    $('#slider').serialScroll(scrollOptions);

    // now apply localScroll to hook any other arbitrary links to trigger the effect
    $.localScroll(scrollOptions);

    // finally, if the URL has a hash, move the slider in to position, 
    // setting the duration to 1 because I don't want it to scroll in the
    // very first page load.  We don't always need this, but it ensures
    // the positioning is absolutely spot on when the pages loads.
    scrollOptions.duration = 1;
    $.localScroll.hash(scrollOptions);
});
//---------------------------------------------------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------------------------------------------------
function metaLoad(obj){
	var my_obj = obj;
	var my_qt = $("#qt");
	var my_w = $("#qt").width();
}
//---------------------------------------------------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------------------------------------------------
function playVideo(e){
	var hash = $(e).attr('vid');
	
	$('.spotmenu').removeClass("selected");
	$(e).addClass("selected");
	
	var video_id = hash.substring((hash.lastIndexOf('/') + 1), hash.length);
	var video_name = $("#" + video_id).attr("vname");
	var video_path = $("#" + video_id).attr("vpath");
	var mimetype = $("#" + video_id).attr("vmime_type");
	var credit = $("#" + video_id).attr("vcredit");
	var editor = $("#" + video_id).attr("veditor");
	
	var sid = Math.random();

		
	$.ajax({type: "GET", url: "video.php", async:false, data: ({sid:sid, mimeType: mimetype, path: video_path, editor: editor, name : video_name, id:video_id, credit:credit }), dataType: "html", success: function(results){
			$(".vid_content").html(results);
			$(".vid_content").css("width","678px");	
			$('.credit','#vid_credits').html( $('#credits_'+video_id).html());
			$('#credits_'+video_id).css({"dispay":"none"});
		}
	});
	
}
//---------------------------------------------------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------------------------------------------------
function getReels(eid, editor){
	
	$.ajax({ type: "GET", url: "reels.php", async:false, data: ({id : eid, editor:editor }), dataType: "html", success: function(results){
			if(results != ''){
				$('#feautured_spots').append(results);
				initReelMenu();
				$('#featured_spots').attr('style','');
				$('#featured_spots').css('overflow-y','auto');
				$(".spotmenu","#reelsmenu").bind('click', function(e){ playVideo(this); } );
			}
		}
	 });
}
//---------------------------------------------------------------------------------------------------------------------------------------------
// init additional reels menu
//---------------------------------------------------------------------------------------------------------------------------------------------
function initReelMenu() {
  $('#reelsmenu ul').hide();
  $('#reelsmenu li a').click(
    function() {
        $(this).next().slideToggle('normal');
      }
    );
 }