var mouseOverSourceBackup="";
var pathJQuery = location.protocol + '//' + location.host + (location.port ? ':' + location.port : '') + '/media/js/jquery.js';

function popupSchmal(url) {
   var popUpWindows = window.open(url,'popupSchmal','width=435,height=280,resizable=yes,menubar=no,toolbar=no,scrollbars=no');
   popUpWindows.focus();
}
function contentSchmalPopup(url) {
   var popUpWindows = window.open(url,'contentschmal','width=435,height=480,resizable=yes,menubar=no,toolbar=no,scrollbars=yes');
   popUpWindows.focus();
}
function tagesplanerpopup(url) {
   var popUpWindows = window.open(url,'popupSchmal','width=435,height=310,resizable=yes,menubar=no,toolbar=no,scrollbars=no');
   popUpWindows.focus();
}
function programmsuchepopup(url) {
   var popUpWindows = window.open(url,'popupSchmal','width=750,height=600,resizable=yes,menubar=yes,toolbar=yes,scrollbars=yes');
   popUpWindows.focus();
}
function ticketonlinepopup(url) {
   var popUpWindows = window.open(url,'popupSchmal','width=485,height=600,resizable=yes,menubar=yes,toolbar=yes,scrollbars=no');
   popUpWindows.focus();
}
function printpopup(url) {
   var popUpWindows = window.open(url,'printpopup','width=640,height=570,resizable=yes,menubar=yes,toolbar=yes,scrollbars=yes');
   popUpWindows.focus();
}
function streamingpopup(url) {
   var popUpWindows = window.open(url,'streamingpopup','width=707,height=665,resizable=yes,menubar=no,scrollbars=no');
   popUpWindows.focus();
}

function thumbspopup(url) {
   var popUpWindows = window.open(url,'printpopup','width=655,height=560,resizable=yes,menubar=no,scrollbars=yes');
   popUpWindows.focus();
}

function contentpopup(url) {
   var popUpWindows = window.open(url,'printpopup','width=652,height=570,resizable=yes,menubar=yes,scrollbars=yes');
   popUpWindows.focus();
}

function famPopup(url) {
   var popUpWindows = window.open(url,'famPopup','width=645,height=570,menubar=no,resizable=yes,scrollbars=yes');
   popUpWindows.focus();
}

function efmPopup(url) {
   var popUpWindows = window.open(url,'efmPopup','width=735,height=670,menubar=no,resizable=yes,scrollbars=yes');
   popUpWindows.focus();
}

function preLoad(){
   for(var i=0;i<preLoadSrc.length;i++){
   	if(preLoadSrc[i+1] && (typeof preLoadSrc[i+1]) != "undefined") {
      		preLoadImages[i] = new Image();
      		preLoadImages[i].src = preLoadSrc[i+1];
      	}
   }
}
function imagePreload(imagePreloadSrc){
   for(var i=0;i<imagePreloadSrc.length;i++){
   	if(preLoadSrc[i+1] && (typeof preLoadSrc[i+1]) != "undefined") {
      		preLoadImages[i] = new Image();
      		preLoadImages[i].src = imagePreloadSrc[i+1];
      	}
   }
   return true;
}

function externalLinkOver(id,img1,img2){
   document.getElementById("ar"+id).src = img1;
   document.getElementById("tx"+id).src = img2;
}

function maximizeHeight(el) {
	el.style.height = el.parentNode.offsetHeight - el.offsetTop + 'px';
}


// lazyload jquery and execute passed functions on dom-ready
var useJQuery = function useJQuery(fn) {
	jQuery(fn);
};


// creates a layer on clicking the specified link
var createPopupLayer = function createPopupLayer() {
	var lastLayer;
	return function (link, content, onClick) {
		link = $(link);
		var toLoad = true;
		var layer = $('<div class="popupLayer" style="display:none;">' +
				'<a href="#" class="close" onclick="jQuery(this).parent().hide();return false;" title="">Close</a>' +
				'</div>');
		var inner = $('<div class="content" />');
		layer.append(inner);
		$(document.body).append(layer);
		link.click(function (e) {
			var offset = link.offset();
			var top;
			var left;
			// company profile of exhibitors list and programmsuche
			if (link.hasClass('profile')) {
				left = offset.left;
				top = offset.top + link.height() + 1;
			// exhibitor details of screening schedule
			} else if (link.hasClass('salesContact')) {
				top = e.pageY + 10;
				left = e.pageX - 208;
			} else if (link.hasClass('acquisitionDetails')) {
				left = link.parent().offset().left;
				top = link.parent().offset().top;
				layer.addClass('acquisitionDetailsLayer');
			} else {
				top = e.pageY - 30;
				left = e.pageX - 208;
			}
			if (toLoad) {
				toLoad = false;
				if (typeof content == 'function') {
					inner.append(content.call(inner, link));
				} else {
					inner.append($(content));
				}
			}
			if (lastLayer) {
				lastLayer.hide();
			}
			lastLayer = layer.css({top: top + 'px', left: left + 'px'}).show();
			if (onClick) {
				onClick.call(inner, link);
			}
			return false;
		});
		return layer;
	};
}();

// primitive selector function: return first found element by id/tag/class in context of another element
var getTag = function () {
	var regexClass = /\.(\w+)/;
	var regexId = /#(\w+)/;
	var regexEl = /^(\w+)/;
	function getText(regex, text) {
		var parts = regex.exec(text);
		if (parts && parts[1]) {
			return parts[1];
		}
		return '';
	}
	function getTag(from, selector) {
		if (selector == undefined) {
			selector = from;
			from = document.body;
		}
		var id = getText(regexId, selector);
		var el = getText(regexEl, selector);
		var cl = getText(regexClass, selector);
		var result = [];
		if (id) {
			result = from.getElementById(id);
		} else if (el) {
			result = from.getElementsByTagName(el)[0];
		} else if (cl) {
			throw "Not implemented yet";
		}
		return result;
	}
	return getTag;
}();

// find first image in el, set src to src
var switchIcon = function () {
	function switchIcon(el, src, event) {
		getTag(el, 'img').src = src;
	}
	return switchIcon;
}();

function log(msg) {
	if (typeof console != 'undefined') {
		console.log(msg);
	}
}

function selectBrowseInput(input, defaultText, event) {
	var result;
	switch (event.type) {
		case 'focus':
			if (input.value == defaultText) {
				input.value = '';
			}
			break;
		case 'blur':
			if (input.value == '') {
				input.value = defaultText;
			}
			break;
		default:
			break;
	}
	return result;
}

// wait max 3 seconds until a global property is present, else fail silently
var waitFor = function (prop, fn) {
	var calls = 0;
	return function () {
		var target = this;
		var args = arguments;
		var interval = setInterval(function () {
			if (window[prop]) {
				clearInterval(interval);
				fn.apply(target, args);
			}
			if (calls++ > 100) {
				clearInterval(interval);
			}
		}, 100);
	};
};

var TrackingHelper = {
	projectPrefix: 'EFM',
	searchCategory: '',

	downloadEvent: waitFor('etrackerConfigured', function (file, tags) {
		ET_Event.download(escape(this.projectPrefix + '/' + file), escape(tags));
	}),

	linkEvent: waitFor('etrackerConfigured', function (link, tags) {
		ET_Event.link(escape(this.projectPrefix + '/' + file), escape(tags));
	}),

	trackVcard: function (object, cat) {
		this.eventStart(cat, object, 'vCard');
	},

	trackFilmDetailsLink: function (object, cat) {
		this.eventStart(cat, object, 'Filmdatenblatt');
	},

	trackCompanyProfileLink: function (object, cat) {
		this.eventStart(cat, object, 'Firmenprofil');
	},

	trackAcquisitionDetailsLink: function (object) {
		this.eventStart('Buyers', object, 'Acquisition details');
	},
	
	trackIcalEvent: function (type, cat) {
		cat = cat || this.searchCategory;
		this.eventStart(cat, type, 'iCal');
	},
	
	trackDailyPlannerEvent: function (type, cat) {
		cat = cat || this.searchCategory;
		this.eventStart(cat, type, 'DailyPlanner');
	},
	
	trackToolboxCalendarEvent: function (day) {
		cat = 'ProgramsearchEFM';
		day = day + '.02.';
		this.eventStart(cat, day, 'ToolboxCalendar');
	},
	
	trackSortEvent: function (field, cat) {
		cat = cat || this.searchCategory;
		this.eventStart(cat, field, 'Sort');
	},
	
	trackWorldSalesEvent: function (id, cat) {
		cat = cat || this.searchCategory;
		this.eventStart(cat, 'WordSales', 'Layer');
	},
	
	getFileName: function (file) {
		file = file.replace(/^.*\//, '');
		file = file.replace(/(_Galerie[QH]|)\.jpg$/, '');
		return file;
	},
	
	trackPlayerEvent: function (film, action, where) {
		where = where || this.trackingTags || '';
		this.eventStart('Video', this.getFileName(film), action, where);
	},

	/* currently does not submit the search term */
	trackSearchEvent: function (object, cat, initField) {
		initField = initField || '';
		this.eventStart(cat, initField, 'Search');
	},

	eventStart: waitFor('etrackerConfigured', function (cat, obj, action, tags) {
		tags = tags || '';
		ET_Event.eventStart(cat, obj, action, tags);
	})

};

