// Adfinem.net Website Copyright (C)

var ANCHOR_PREFIX = 'go-to-';

var INDICATOR_ID = 'dynamic-loading'
var INDICATOR = '<span class="' + INDICATOR_ID + '"></span>';

function afterAjax() {
	//defaultInputValues();
	newsRotation();
	dynamicLinks();
	$('.' + INDICATOR_ID).each( function (idx) {
		$(this).remove();
	});
	setupScroller();
	setupGallery();
}

function setupScroller() {
	// Taken from: http://valums.com/scroll-menu-jquery/
    //Get our elements for faster access and set overlay width
    var div = $('div.sc_menu'),
                 ul = $('ul.sc_menu'),
                 // unordered list's left margin
                 ulPadding = 15;

    //Get menu width
    var divWidth = div.width();

    //Remove scrollbars
    div.css({overflow: 'hidden'});

    //Find last image container
    var lastLi = ul.find('li:last-child');

    //When user move mouse over menu
    div.mousemove(function(e){

      //As images are loaded ul width increases,
      //so we recalculate it each time
      var ulWidth = lastLi[0].offsetLeft + lastLi.outerWidth() + ulPadding;

      var left = (e.pageX - div.offset().left) * (ulWidth-divWidth) / divWidth;
      div.scrollLeft(left);
    });
}

function setupGallery() {
	// setup gellery
	$('.gallery a').lightBox();
}

function newsRotation() {
	$(".news .arowdown").each(
		function(idx) {
			
			$(this).unbind('click');
			$(this).bind(
				"click",
				function () {
					var el = $(this).parent(".news");
					var vis = el.find(".scrolled div:visible:first");
					var next = vis.next("div:hidden:first");
					if (vis.length == 1 && next.length == 1) {
						vis.fadeOut("slow", function() {
							next.fadeIn("slow");
						});
					} else {
						vis.animate({'padding-top':"+=1px"},100).animate({'padding-top':"-=1px"},100);
					}
					return false;
				}
			); 
		}
    );
	$(".news .arowup").each(
		function(idx) {
			$(this).unbind('click');
			$(this).bind(
				"click",
				function () {
					var el = $(this).parent(".news");
					var vis = el.find(".scrolled div:visible:first");
					var prev = vis.prev("div:hidden:first");
					if (vis.length == 1 && prev.length == 1) {
						vis.fadeOut("slow", function() {
							prev.fadeIn("slow");
						});
					} else {
						vis.animate({'padding-top':"-=1px"},100).animate({'padding-top':"+=1px"},100);
					}
					return false;
				}
			); 
		}
    );
}

function getContent(actionId) {
	xajax_XAjaxHandler.proceed({ data: { action: actionId, 'dest-id': 'content' } });
	if (window.pageTracker) {
		// let google know 
		window.pageTracker._trackPageview("/" + actionId);
	}
}

function dynamicLinks() {
	$("a.dynamic").each(
		function( idx ) {
			$(this).unbind('click');
			$(this).bind (
				"click",
				function() {
					el = $(this);
					destination = el.attr('href').split('/').pop();
					// commented out since HashChange handler will get the content 
					//getContent(destination);
					var newHash = ANCHOR_PREFIX + destination;
					if (window.location.hash != '#' + newHash) {
						el.append(INDICATOR);
						window.location.hash = ANCHOR_PREFIX + destination;
					}
					return false;
				}
			);
		}
	);
	$("input.dynamic").each(
		function( idx ) {
			$(this).unbind('click');
			$(this).bind (
				"click",
				function() {
					var el = $(this);
					var parent = el.parents('form:first');
					el.after(INDICATOR);
					xajax_XAjaxHandler.proceed(xajax.getFormValues(parent.attr("id")));
					return false;
				}
			);
		}
	);	
}

function defaultInputValues() {
	//return; // not using because livequery does not match correctly when divs content is replaced dynamically.
	
	// most of it taken from: http://hubpages.com/hub/default-text-field-value-disappears-focus-jquery
 	$("input[type=text][title]").livequery(function() {  // livequery does not work pretty well with this kind of xajax :/
   		$(this).val($(this).attr("title"));  
   		if($.trim($(this).val()) == "")  
     		$(this).val($(this).attr("title"));  
   		$(this)  
   			.focus(function() {  
       			if($(this).val() == $(this).attr("title")) $(this).val("");  
     		})  
     		.blur(function() {  
       			if($.trim($(this).val()) == "") $(this).val($(this).attr("title"));  
     		});  
 	});
}

function anchoredPages(loading) {
	// check whether someone has bookmarked (ajax originating) anchor,
	// if so, get the content for it.
	actionAnchor = window.location.hash;
	if (actionAnchor && actionAnchor.match("^#" + ANCHOR_PREFIX) == "#" + ANCHOR_PREFIX || (typeof(loading) == 'undefined' && (actionAnchor == "" || actionAnchor == "#"))) {
		getContent(actionAnchor.split(ANCHOR_PREFIX).pop());
	}
}

$(document).ready(function() {
	//defaultInputValues();
	dynamicLinks();	
	newsRotation();
	anchoredPages(true);
	$(window).hashchange(function() { anchoredPages(); } );
	setupScroller();
	setupGallery();
});



