// JavaScript Document

$(function(){
	
	initializePage()

	// HotProjects				
	var hpWidth = $('#hotProjectsList li').width()
	var active = $('#hotProjectsList li:first');
	var delay = 330;
	var dHeight = 25;
	var top = dHeight/1.6;
	
			
	active.animate({opacity:1,left:0, top:0}, 0);
	//active.children('img').animate({height: "+="+dHeight, top:0}, 0);	
		
	function checkSliderArrows(active){
 
	  (active.prev().length == 0) ? $('#hotProjects_LeftArrow').hide()
								  : $('#hotProjects_LeftArrow').fadeIn(200);
								  
	  (active.next().length == 0) ? $('#hotProjects_RightArrow').hide()
								  : $('#hotProjects_RightArrow').fadeIn(200);					
	}
	
	checkSliderArrows(active, 0);
	$('#hotProjects_LeftArrow').click(function(){
		prev = active;
		active = active.prev();
		
		active.animate({opacity:0}, 0).animate({opacity:1}, delay/1.6);
		//active.children('img').animate({height: "-="+dHeight, top:top}, 0).animate({height: "+="+dHeight, top:0}, delay);
		prev.animate({opacity:0}, delay/1.6);
		//prev.children('img').animate({height: "-="+dHeight, top:top}, delay).animate({height: "+="+dHeight, top:0}, 0);
		$('#hotProjectsList').animate({left:"+="+hpWidth}, delay);
				
		checkSliderArrows(active);
		return false;
	})
	$('#hotProjects_RightArrow').click(function(){
		prev = active;
		active = active.next();
		
		active.animate({opacity:0}, 0).animate({opacity:1}, delay/1.6);
		//active.children('img').animate({height: "-="+dHeight, top:top}, 0).animate({height: "+="+dHeight, top:0}, delay);
		prev.animate({opacity:0}, delay/1.6);
		//prev.children('img').animate({height: "-="+dHeight, top:top}, delay).animate({height: "+="+dHeight, top:0}, 0);
		$('#hotProjectsList').animate({left:"-="+hpWidth}, delay);
				
		checkSliderArrows(active);
		return false;
	})


	$('#right-menu a').mouseover(function(){
			$(this).stop(true).animate({left:"-9px"}, 140)
	})
		
	$('#right-menu a').mouseleave(function(){
		$(this).animate({left:4}, 200).animate({left:0}, 190);
	})
})


function getFlashVersion(){
// ie
    try {
        try {
          // avoid fp6 minor version lookup issues
          // see: http://blog.deconcept.com/2006/01/11/getvariable-setvariable-crash-internet-explorer-flash-6/
          var axo = new ActiveXObject('ShockwaveFlash.ShockwaveFlash.6');
          try { axo.AllowScriptAccess = 'always'; }
          catch(e) { return '6,0,0'; }
        } 
        catch(e) {}
        return new ActiveXObject('ShockwaveFlash.ShockwaveFlash').GetVariable('$version').replace(/\D+/g, ',').match(/^,?(.+),?$/)[1];        
        // other browsers
    } 
    catch(e) {
        try {
          if(navigator.mimeTypes["application/x-shockwave-flash"].enabledPlugin){
            return (navigator.plugins["Shockwave Flash 2.0"] || navigator.plugins["Shockwave Flash"]).description.replace(/\D+/g, ",").match(/^,?(.+),?$/)[1];
          }
        } catch(e) {}
    }
    return '0,0,0';
}

function initializePage(){
	//var ref = getUrlValue("ref"); // epistrefei thn timh ths parametrou ref pou vrisketai sto hash	
	//alert(ref);
	var hash = window.location.hash.toString();
	
	if(hash.indexOf("ref")){		
		$('#mainPage').empty();
	}
	
	$.history.init(Router);
	
	_init();
	
	$('#whiteFixedOverlay').hide();
}

function isValidAjaxPageUrl(url)
{
    var urlRegex = new RegExp("^/+ref+/+[0-9]{1,}/lang+/", "i");
    return urlRegex.test(url);
}

function Router(url){
	var href = window.location.pathname.toString().replace(Root, "").replace('/', '')
	//alert(href)
	
	if(url.indexOf("ref") >= 0){
		loadAjaxPage(url);
		document.title = Title;
	}	
	else if(href.indexOf("intro") >= 0){
		
		swfobject.embedSWF(Root + "/public/flash/intro.swf", 
				"flash-intro", "100%", "100%", "10.0.0", 
				"/js/expressInstall.swf", 
				flashvars, params, attributes);
		
		//doResize();
		var wHeight = $('#wrap').attr('offsetHeight')
		var bHeight = $('body').height();
		var newHeight = "100%";
	
		if(wHeight > bHeight){
			newHeight = wHeight + 10;// - bHeight
		}		
		$('#flashIntro').css({top:0, height: newHeight, width:"100%"}).show();
		$('#skipIntro').show();		
	}
	else{
		loadAjaxPage(href);
	}
}

function getUrlValue(param){
	//pattern = "ref/"
	var value = null;
	var href = window.location.pathname.toString();
	var hash = window.location.hash.toString().replace('#', '');

	hash = decodeURIComponent(hash);

	if(hash.indexOf(param) != -1){
		var pos = hash.indexOf(param) + param.length;
		var rest = hash.substr(pos);
		//alert(rest);
		
		if(rest.substr(0,1) == "/"){
			rest = rest.substr(1)
			//alert(rest)
			end = rest.indexOf('/');
			if(end == -1){
				end = rest.length;
			}
			value = rest.substr(0, end);		
		}		
	}
	else if(href.indexOf(param) != -1){
		var pos = href.indexOf(param) + param.length;
		var rest = href.substr(pos);		
		
		if(rest.substr(0,1) == "/"){
			rest = rest.substr(1)
			//alert(rest)
			end = rest.indexOf('/');
			if(end == -1){
				end = rest.length;
			}
			value = rest.substr(0, end);		
		}			
	}
	
	//alert(value)
	
	return value;
}

/*
function setSelectedButton() {
	try{								
		var t = swfobject.getObjectById("topmenu");
		//var t = document.getElementById("topmenu");
		ref = parseInt(getUrlValue("ref"));
		t.setSelectedIndex(ref);
	}
	catch(e){
		//alert(e);	
	}
}
*/


function getSelectedIndex() {
		
		var selected = 0;
		var ref = getUrlValue("ref");
		
		if(ref){
			selected = parseInt(ref);
		}				
		
		return selected;
}


// It Calls Ajax Pages
function loadAjaxPage(ref){

	url = Root + '/index/ajax/' + ref;
	
	var page = url.replace('index/index','index/ajax');
	page = page.replace('//', '/');
	
	if(isNaN(page)){
		var height = $('#mainPage').height()
		var delay = 130;
		
		$('#mainPage').animate({left:20, opacity:0}, delay, function(){					
			//alert(height)
			
			$(this).hide().animate({left:0, opacity:0}, 0);
			$('#mainPage:hidden').parent().addClass('isloading');
			$('#mainPage-flash-loader-overlay').show();
			$('#mainPage').load(page, function(){							
								var newHeight = $('#mainPage').height()
								
								if(height > newHeight){
									$('html, body').animate({scrollTop:0}, 50);
								}
								//alert(newHeight)
								crossFadeImages(450, 0);								
								$('#mainPageContainer').delay(0).removeClass('isloading').animate({height: newHeight}, 450)					
								$('#mainPage').show().animate({left:10, opacity:1}, 250)
								_init();								
								$('#mainPage-flash-loader-overlay').hide();									
																                                
                                ref = parseInt(getUrlValue("ref"));
                
                                if(HtmlTopMenu.exists()){
                                    HtmlTopMenu.getSelectedIndex(ref);                    
                                }
                                else{
                                    try{
                                        var t = swfobject.getObjectById("topmenu");                        
                                        t.setSelectedIndex(ref);
                                    }
                                    catch(e){
                                        //alert(e);	
                                    }
                                } 
							
							}).ajaxError(function(e, xhr, ajaxOptions, error) {
								//$(this).html('<iframe src="'+page+'"></iframe>');
								$(this).html("An Error Has Occurred");
								$('#mainPage-flash-loader-overlay').fadeOut();								
							});
			
		})
	}
	else{
		alert('loadAjaxPage: Not valid parameter');
	}	
}

function callPage(url) {
	url = '/ref/' + url + '/lang/' + Lang;
	$.history.load(url);
}


// ekteleitai 1o
function changeBGColor() {
	//alert('changeBGColor')	
	$("#flashIntro").css({"background-color" : "transparent"});
}

// ekteleitai 2o
function resizeDivHor() {
	//alert('resizeDivHor');	
	//var brWidth = $(document).width();
	//$("#flashIntro").css({"left" : "50%"});
	//$("#flashIntro").css({"margin-left" : "-450px"});
	//$("#flashIntro").width("900px");
	$("#skipIntro").hide();
	$("#flashIntro").css({top:14});
	//$("#flashIntro").animate({marginTop:14}, 50);
	//$('html, body').animate({scrollTop:0}, 0);
	//$("#flashIntro").css({"background-color" : "transparent"})
}

// ekteleitai 3o
function resizeDivVer() {
	//alert('resizeDivVer')
	//$("#flashIntro").height(190);
}


function hideIntro(){
	$("#flashIntro").delay(200).animate({left:1, opacity:0}, 350, function(){
		$(this).hide().empty()
	});
}


function skipIntro(){
	//window.location.href = Root + '/index/load/ref/1/lang/' + Lang;
}

function showMoreNewsMaker(){
	var lh = parseInt($('ul.news-list li').css('line-height'));
	var dlh = lh * 2;
	var duration = 250;
    var hash = window.location.hash.toString().replace("#","");
    var except_id = "_" + hash;
        
	$('ul.news-list li').each(function(){
		var h = $(this).children('.text').height();
		var id = $(this).attr("id");
        
		if($(this).attr('id') == hash){                    
        }
        else if(h > dlh && dlh!=NaN && id!=except_id){
			$(this).addClass('tooBig');
			$(this).children('.text').height(dlh);
			$(this).children('.more').show().click(function(){
                            var active = $('ul.news-list li.active');
                            active.children('.text').animate({height:dlh},duration);
                            active.children('.more').animate({opacity:1, height:'toggle'}, duration/2);
                            active.removeClass('active');

                            active = $(this).parent('li');
                            active.addClass('active');
                            active.children('.text').animate({height:h},duration);//slideDown();
                            active.children('.more').animate({opacity:0, height:'toggle'}, duration/2);
			})
		}				
	});
    
    if($("#"+except_id).length == 1){
        var pos_y = $('#content').position().top + $("#"+except_id).position().top;
        window.scrollTo(0, pos_y);    
        //console.log(pos_y);    
    }    
}


function openNewToRead($new_more_link){        
    var lh = parseInt($('ul.news-list li').css('line-height'));
    var dlh = lh * 2;    
    var active = $('ul.news-list li.active');
    var h = $new_more_link.parent().height();
    var duration = 250;
    
    active.children('.text').animate({height:dlh},duration);
    active.children('.more').animate({opacity:1, height:'toggle'}, duration/2);
    active.removeClass('active');

    active = $new_more_link.parent('li');
    active.addClass('active');
    active.children('.text').animate({height:h},duration);//slideDown();
    active.children('.more').animate({opacity:0, height:'toggle'}, duration/2);      
}


function _init(){
	if($('#contact-form-loader').length > 0 ){
		swfobject.embedSWF(Root + "/public/flash/loader-mail.swf", 
		"contact-form-flash-loader", "100%", "100%", "10.0.0", 
		"/js/expressInstall.swf", 
		flashvars, params, attributes);	
	}

	//$('a[rel="history"]')
	$('a[rel="ajaxPage"]').live('click', function(){
		var ref = $(this).attr('href').toString();		
		ref = ref.replace(Root + '/index/load', '');
		$.history.load(ref);

		return false;
	})
		
		
	showMoreNewsMaker();
	crossFadeImages(0,0);
}

function loadBanners(){
	var duration = 300;
	var delay = 500;
	
	if($('#banners').length > 0){
		//alert("yparxoyn banners!");
		var left_src = $('#banners img:first').attr('src');
		var right_src = $('#banners img:last').attr('src');
		
		$('#leftBanner').html('<img src="'+left_src+'"alt=""/>').delay(delay).fadeIn(duration);
		$('#rightBanner').html('<img src="'+right_src+'"alt=""/>').delay(delay).fadeIn(duration);
	}
	else{
		$('.banner').hide().empty();//fadeOut(800);//.empty();
	}
}

function test(){
	alert('it works!');	
	return false;
}


function crossFadeImages(time, delay){
	var mainImage = $('#mainImageHidden').html();
	/*
	if($('#mainImageHidden').length == 0){
		mainImage = '<br/><br/><br/><br/>No foto found!';	
	}*/	

	if($('#mainImageList li').is(':visible')){
		
		var currImage = $('#mainImageList li:visible');
		var nextImage = $('#mainImageList li:hidden')
		
		nextImage.html(mainImage);
		currImage.delay(delay).fadeOut(time);
		nextImage.delay(delay).fadeIn(time);					
	}
	else{
		$('#mainImageList li:first').html(mainImage).show();
	}	
}


function gotoMaternal(){
	window.location.href = Root + "/index/load/ref/41/lang/"+Lang;
}

function gotoUpdates(){
	window.open("http://update.noon.gr/Patches", "updates"); 
//	window.location.href= "http://update.noon.gr/Patches";
}

function gotoIntro(){
	window.location.href = Root + "#/ref/intro/lang/" + Lang;
}

