var pathJQuery = '/media/js/jquery-132min_1.js';
var mouseOverSourceBackup="";

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=555,height=650,resizable=yes,menubar=no,toolbar=no,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 streamingpopup2008(url) {
   var popUpWindows = window.open(url,'streamingpopup','width=967,height=780,resizable=yes,menubar=no,scrollbars=yes');
   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=653,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;
}

// lazyload jquery and execute passed functions on dom-ready
var useJQuery = function useJQuery() {
	var loading = false;
	var loaded = false;
	var queue = [];
	var script = document.createElement('script');
    function ready() {
    	loading = false;
    	loaded = true;
		jQuery(document).ready(function () {
			for (var i = 0, l = queue.length; i < l; ++i) {
				queue.shift().call(document, jQuery);
			}
		});
	}
	return function (fn) {
		if (loading) {
			queue.push(fn);
		} else if (loaded) {
			fn.call(document, jQuery);
		} else {
			loading = true;
			// which browser
		    if (!/MSIE/.test(navigator.userAgent)) {
				script.onload = ready;
			} else {
				script.onreadystatechange = function () {
			        if (this.readyState === 'loaded' || this.readyState === 'complete') {
						ready();
			        }
			    }
			}
			// append script to dom to start loading
			script.src = pathJQuery;
			document.getElementsByTagName('head')[0].appendChild(script);
			queue.push(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 {
				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;
	};
}();

// logs to firebug, if present
function log(msg) {
	if (typeof console != 'undefined') {
		console.log(msg);
	}
}

// handler to insert a default text into textboxes
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;
}

// 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;
}();



try {
	// prevents background flickering in ie
	if (/MSIE [67]/.test(navigator.userAgent))
		document.execCommand("BackgroundImageCache", false, true);
} catch (ex) {}


//wait max 10 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: 'IFB',
	searchCategory: '',
	trackingTags: '',
	
	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 + '/' + link), 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');
	},
	
	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');
	},
	
	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');
	},

	/* currently does not submit the search term */
	trackSearchEvent: function (object, cat, initField) {
		initField = initField || '';
		this.eventStart(cat, initField, 'Search');
	},
	
	getFileName: function (file) {
		file = file.replace(/^.*\//, '');
		file = file.replace(/(_Galerie[QH]|)\.jpg$/, '');
		return file;
	},
	
	normalize: function (s) {
		return s.replace(/\s+/g, ' ').replace(/(^\s)|(\s$)/g, '');
	},
	
	trackContentModuleEvent: function (moduleTitle, action, where) {
		where = where || this.trackingTags || '';
		this.eventStart('Contentmodul', this.normalize(moduleTitle), action, where);
	},
	
	trackPlayerEvent: function (film, action, where) {
		where = where || this.trackingTags || '';
		this.eventStart('Video', this.getFileName(film), action, where);
	},
	
	trackImageGalleryEvent: function (image, action, where) {
		where = where || this.trackingTags || '';
		this.eventStart('Bildergalerie', this.getFileName(image), action, where);
	},
	
	trackLightboxEvent: function (image, action, where) {
		where = where || this.trackingTags || '';
		this.eventStart('Lightbox', this.getFileName(image), action, where);
	},
	
	eventStart: waitFor('etrackerConfigured', function (cat, obj, action, tags) {
		tags = tags || '';
		ET_Event.eventStart(cat, this.normalize(obj), action, tags);
	})
	
};
