function _rollover(obj,r) { _assign(obj,obj.src,r); }
function _assign(obj,s,r) {
	obj.onmouseover = function() {
		$(obj).attr("src",r);
	};
	obj.onmouseout = function() { obj.src = s; };
	obj.border=0; obj.onload='';
}

function changePositionFixed(obj, h) {
	$(obj).css('top', h);
}



var ismobile=navigator.userAgent.match(/(iPad)|(iPhone)|(iPod)|(android)|(webOS)/i);
if (ismobile){
	//var no_animate = true;
    
	$(".ied").css({
		"background-size":"cover"
	});

  	$(function(){
		$("#menu").css("position","relative");
    	$("#footer").css("position","relative");
	});

	$(document).bind('scroll', function() {
		/*$("#test").html('Inner Height:'+window.innerHeight+'<br />Scroll Y:'+window.scrollY);
		changePositionFixed('#menu',  0 + window.scrollY + "px");
		changePositionFixed('#footer', window.innerHeight + window.scrollY - 44 + "px");*/
		changePositionFixed('#prev',  window.innerHeight/2 + window.scrollY - 61 + "px");
		changePositionFixed('#next',  window.innerHeight/2 + window.scrollY - 61 + "px");
	});
}
(function($) {

	if(!document.defaultView || !document.defaultView.getComputedStyle){ // IE6-IE8
		var oldCurCSS = $.curCSS;
		$.curCSS = function(elem, name, force){
			if(name === 'background-position'){
				name = 'backgroundPosition';
			}
			if(name !== 'backgroundPosition' || !elem.currentStyle || elem.currentStyle[ name ]){
				return oldCurCSS.apply(this, arguments);
			}
			var style = elem.style;
			if ( !force && style && style[ name ] ){
				return style[ name ];
			}
			return oldCurCSS(elem, 'backgroundPositionX', force) +' '+ oldCurCSS(elem, 'backgroundPositionY', force);
		};
	}

	var oldAnim = $.fn.animate;
	$.fn.animate = function(prop){
		if('background-position' in prop){
			prop.backgroundPosition = prop['background-position'];
			delete prop['background-position'];
		}
		if('backgroundPosition' in prop){
			prop.backgroundPosition = '('+ prop.backgroundPosition;
		}
		return oldAnim.apply(this, arguments);
	};

	function toArray(strg){
		strg = strg.replace(/left|top/g,'0px');
		strg = strg.replace(/right|bottom/g,'100%');
		strg = strg.replace(/([0-9\.]+)(\s|\)|$)/g,"$1px$2");
		var res = strg.match(/(-?[0-9\.]+)(px|\%|em|pt)\s(-?[0-9\.]+)(px|\%|em|pt)/);
		return [parseFloat(res[1],10),res[2],parseFloat(res[3],10),res[4]];
	}

	$.fx.step. backgroundPosition = function(fx) {
		if (!fx.bgPosReady) {
			var start = $.curCSS(fx.elem,'backgroundPosition');
			if(!start){
				//FF2 no inline-style fallback
				start = '0px 0px';
			}

			start = toArray(start);
			fx.start = [start[0],start[2]];
			var end = toArray(fx.end);
			fx.end = [end[0],end[2]];

			fx.unit = [end[1],end[3]];
			fx.bgPosReady = true;
		}
		//return;
		var nowPosX = [];
		nowPosX[0] = ((fx.end[0] - fx.start[0]) * fx.pos) + fx.start[0] + fx.unit[0];
		nowPosX[1] = ((fx.end[1] - fx.start[1]) * fx.pos) + fx.start[1] + fx.unit[1];
		fx.elem.style.backgroundPosition = nowPosX[0]+' '+nowPosX[1];

	};
})(jQuery);

var bgpos = 0;
var hoverColour = "#ff0000";
var running=false;
/*HTML5 History API*/
function load_content(url,nav_type){
	if (running) return;
	running=true;

	//update content
    $.get(php_self+"?a=next&url="+url, function(data){
		if (data.nextpage!=null){
			eval("var t=data.nextpage.title."+lang+";");

			$("#next").attr({
				"href":data.nextpage.url,
				"rel":t
			}).css("display","");

		}else{
		    $("#next").css("display","none");
		}

		if (data.prevpage!=null){
		    eval("var t=data.prevpage.title."+lang+";");
		    $("#prev").attr({
				"href":data.prevpage.url,
				"rel":t
			}).css("display","");
		}else{
		    $("#prev").css("display","none");
		}

		if (nav_type=='next'){
			var mleft = 10;
			bgpos-=100;
		}
		else{
		    var mleft = -10;
	        bgpos+=100;
		}

		var ww = $('.text').width() + $(window).width()*.1;

		$('.content').animate({ 'left' : -1*ww }, 500, function() {
		    eval("var subtitle1=data.subtitle1."+lang+";");
	     	eval("var subtitle2=data.subtitle2."+lang+";");
		    eval("var content=data.content."+lang+";");

			if (subtitle1=='')
				subtitle1 = 'New Page Title1';

		    if (subtitle2=='')
				subtitle2 = 'New Page Title2';

			if (content=='')
				content = 'Coming soon...';

		    $(".c h1").html(subtitle1);
		    $(".c h2").html(subtitle2);
	        $(".c .text").html(content);
	    	$('.content').css('left', $(window).width()).animate({ 'left' : mleft }, 500);
		});


			$('body').animate({
				'backgroundPosition' :bgpos+'px 0px'
			},1200, function(){
				running=false;
			});
	}, "json");
	//
}


$(function() {
	if (scroll){
		// check to see if the browser supports the HTML5 history API
	    if (window.history && history.pushState)
	    {
			var first_load = true;

            $("#prev").click(function(event){

			   //stop page load to <a href>

			   event.preventDefault();

			   //get current topic id
			   var page = $(this).attr('href');
			   var title= $(this).attr('rel');

			   //update content
			   load_content(page,'prev');

				document.title = title;
				// update our address bar
				history.pushState(null, title, page);

				// and change the fact that this is not the original page load
	            first_load = false;
			});

			$("#next").click(function(event){
			   //stop page load to <a href>

			   event.preventDefault();

			   //get current topic id
			   var page = $(this).attr('href');
			   var title= $(this).attr('rel');

			   //update content
			   load_content(page,'next');

                document.title = title;
				// update our address bar
				history.pushState(null, title, page);

				// and change the fact that this is not the original page load
	            first_load = false;
			});

			// ! Update our content when the user hits the back/forward button
	        $(window).bind('popstate', function(event){
	            // only do something if it's not the first page load
	            if (!first_load)
	            {
	                // use the load_content function to load our main content, update classes, etc.
	                load_content(location.pathname);
	            }
	        });


	        document.onkeydown = function(e)
			{

				if (e == null){ // ie
					keycode = event.keyCode;
				}else{ // mozilla
					keycode = e.which;
				}

				//-> right key
				if(keycode == 39){
				    load_content($('#next').attr('href'),'next');
				}

				//<- left key
				if(keycode == 37){
		            load_content($('#prev').attr('href'),'prev');
				}
			}
		}
	}else{
		$("body").css({
			"background-Image":"none"
		});
	}


	//logo
	$("#logo").hover(function(){
		$(this).stop().animate({
			opacity:0.3
		});
	}, function(){
		$(this).stop().animate({
			opacity:1
		});
	});




	//Back link
	$("#back_link").hover(function(){
		$(this).animate({
			'backgroundColor':'#d2d2d2',
			'color':'#000'
		});
	}, function(){
		$(this).animate({
			'backgroundColor':'#000',
   			'color':'#fff'
		});
	});

	//menu - first ul
	$("#menu>ul>li>a")
	.hover(function(){
	    $(this).animate({ "paddingLeft":"10px"},100);
		$(this).parent().animate({
			"backgroundPosition":"95% 0"
		},100);
	}, function(){
	    $(this).animate({ "paddingLeft":"0"},100);
        $(this).parent().animate({
			"backgroundPosition":"100% 0"
		},100);
	})
	.click(function(){
		//Hide all submenu
		$(this).parent().siblings()
			.animate({
				backgroundPosition: "120% 0"
			},100,function(){
				$(this).css("background-image","url(images/plus.jpg)");
				$(this).animate({
					backgroundPosition:"100% 0"
				});
			});

	    $(this).parent().siblings().find("ul")
			.fadeOut();

		//Show this submenu
		$(this).parent().css("background-image","url(images/neg.jpg)");
		$(this).parent().find("ul").fadeIn();
	});

	//menu - second ul
	$("#menu>ul>li>ul>li>a").hover(function(){
	    $(this).animate({
                    color:"#000",
					paddingLeft:"10px",
					"font-weight":"bold"
		},100);
	}, function(){
	    $(this).animate({
	        color:"#777",
			paddingLeft:"0",
			"font-weight":"400"
		});
	});

	//open submenu
	$("#menu ul li").css("backgroundImage","url(images/plus.jpg) no-repeat");
	$("#"+page_no).css("backgroundImage","url(images/neg.jpg) no-repeat");
	$("#"+page_no).find("ul").fadeIn();




	//tooltip
    $('#logo-link').tipsy({
		fade: true,
		gravity: 'w'
	});
	/*$('#home').tipsy({
		fade: true,
		gravity: 'e'
	});*/
	$('#news_cat').tipsy({
		fade: true,
		gravity: 'e'
	});
	$('#event_cat').tipsy({
		fade: true,
		gravity: 'e'
	});
    $('.back_link').tipsy({
		fade: true,
		gravity: 'e'
	});
    $('#prev').tipsy({
		fade: true,
		gravity: 'w'
	});
	$('#next').tipsy({
		fade: true,
		gravity: 'e'
	});
	$('#prevProd').tipsy({
		fade: true,
		gravity: 'w'
	});
	$('#nextProd').tipsy({
		fade: true,
		gravity: 'e'
	});
	/*$('.panel a').tipsy({
		fade: true,
		gravity: 'n'
	});*/
});
