/* Developed by Gaurav (gauravbg@iitk.ac.in)*/

gotoURL = function(a){
    //alert(a);
    //window.lo
}
var newsIndex, newsMax, sponsorsIndex, sponsorsMax, updateIndex, updateMax;
$(document).ready(function() {
    //boxInit();
	eventboxInit();
    fboxInit();
    if(jQuery.browser.msie && (jQuery.browser.version<7))
        ie6FluidBoxFix();
});

	updateInit = function(){
		updateIndex = 0;
		updateMax = $('#updatebox div').size();
		$('#updatebox div:eq(0)').show();
		setInterval(updateNext, 3000);
	}
	
	updateNext = function(){
		$('#updatebox div:eq('+updateIndex+')').hide('slow',function(){
			updateIndex = (updateIndex + 1) % updateMax;
			$('#updatebox div:eq('+updateIndex+')').show('slow');
        });
	}
	
	scroller = function(){
		$(".jCarouselLite").jCarouselLite({
		btnNext: ".next",
		btnPrev: ".prev",
		easing: "bounceout",
		speed: 1500,
		visible: 1
		});
	}
	
	nextimage = function(){
		$(".next").click();
		setTimeout(nextimage,2500);
	}

boxInit = function(){
    /*tl = document.createElement('div');
    tr = document.createElement('div');
    bl = document.createElement('div');
    br = document.createElement('div');
    $(tl).attr('class', 'box-top-left');
    $(tr).attr('class', 'box-top-right');
    $(bl).attr('class', 'box-bottom-left');
    $(br).attr('class', 'box-bottom-right');*/
    $('.box').each(function(){
        //this.appendChild(tl);
        ht = $(this).outerHeight();
        wd = $(this).outerWidth();
        $('.box-top-left', this).css('height', ht);
        $('.box-top-right', this).css('height', ht);
        $('.box-bottom-left', this).css('height', ht);
        $('.box-bottom-right', this).css('height', ht);
        $('.box-top-left', this).css('width', wd);
        $('.box-top-right', this).css('width', wd);
        $('.box-bottom-left', this).css('width', wd);
        $('.box-bottom-right', this).css('width', wd);
        $(this).bind('mouseenter', function(i){
            alert($(this).height());
            $('.box-bottom-left', this).css('background-position', '-951px 100%');
            $('.box-bottom-right', this).css('background-position', '100% '+($(this).outerHeight()-630+'px'));
        });
        $(this).bind('mouseleave', function(i){
            $('.box-bottom-left', this).css('background-position', '0% 100%');
            $('.box-bottom-right', this).css('background-position', '100% 100%');
        });
    });
}

ie6FluidBoxFix = function(){
    $('.box-events-left, .box-fluid, .box').each(function(){
        ht = $(this).outerHeight();
        wd = $(this).outerWidth();
        if($(this).attr('id')=='sponsors-box')
            ht = Math.ceil(numberOfSponsors/4)*175+50;
        $('.box-top-left', this).css('height', ht);
        $('.box-top-right', this).css('height', ht);
        $('.box-bottom-left', this).css('height', ht);
        $('.box-bottom-right', this).css('height', ht);
        $('.box-top-left', this).css('width', wd);
        $('.box-top-right', this).css('width', wd);
        $('.box-bottom-left', this).css('width', wd);
        $('.box-bottom-right', this).css('width', wd);
    });
}


fboxInit = function(){
    newsIndex = sponsorsIndex = 0;
    newsMax = $('#news div').size();
    sponsorsMax = $('#sponsors div').size();
    $('#news div:eq(0)').show();
    $('#sponsors div:eq(0)').show();
    setInterval(fBoxNext, 2000);
}
fBoxNext = function(){
    $('#news div:eq('+newsIndex+')').fadeOut('slow',function(){
        newsIndex = (newsIndex + 1) % newsMax;
        $('#news div:eq('+newsIndex+')').fadeIn('slow');
        });
    $('#sponsors div:eq('+sponsorsIndex+')').fadeOut('slow',function(){
        sponsorsIndex = (sponsorsIndex + 1) % sponsorsMax;
        $('#sponsors div:eq('+sponsorsIndex+')').fadeIn('slow');
        });
    
    
}

loginboxInit = function(){
    $('#login-form input.ui').each(function(){
        $(this).addClass('login-form-blur');
        $(this).bind('focus',function(e){
            if(this.value==this.defaultValue)
            {
                this.value='';
                $(this).removeClass('login-form-blur');
                $(this).addClass('login-form-focus');
            }
            
        });
        $(this).bind('blur',function(e){
            if(this.value=='')
            {
                this.value = this.defaultValue;
                $(this).removeClass('login-form-focus');
                $(this).addClass('login-form-blur');
            }
        });
    });
}

eventboxInit = function(){
    $('.event-box').each(function(){
        $(this).bind('click', function(e){
            if(typeof(e.target.href)== 'string' && !/.jpg$/.test(e.target.href))
                $(e.target).click();
            else{
                $('.default-link', this).each(function(){
                    window.location.href = this.href;
                });
            }
        });
        $(this).bind('mouseenter', function(e){
            $(this).css('background-position', '0% top');
            $('div.links', this).css('visibility','visible');
            if($('.default-link', this).length == 0)
                $(this).css('cursor', 'default');
        });
        $(this).bind('mouseleave', function(e){
            $(this).css('background-position', '-350px top');
            $('div.links', this).css('visibility','hidden');
        });
    });
}


var numberOfSponsors = 20;
var numberOfSponsors09 = 19;
generateSponsors = function(){
    for(var i=1; i<=numberOfSponsors;i++)
    {
        $('<div />').addClass('sponsor').css('background-image', 'url(\'images/sponsors/'+i+'.jpg\')').appendTo($('#float-container'));
    }
}

generateSponsors09 = function(){
    for(var i=1; i<=numberOfSponsors09;i++)
    {
        $('<div />').addClass('sponsor').css('background-image', 'url(\'images/sponsors09/'+i+'.jpg\')').appendTo($('#float-container'));
    }
}

sponsorTabInit = function(){
    $('#sponsor-tabs a.first').bind('mouseenter',function(){
        $('#sponsor-tabs a.left').css('background-position', 'center -51px');
    }).bind('mouseleave',function(){
        $('#sponsor-tabs a.left').css('background-position', 'center 0');
    });
    
    $('#sponsor-tabs a.last').bind('mouseenter',function(){
        $('#sponsor-tabs a.right').css('background-position', 'center -51px');
    }).bind('mouseleave',function(){
        $('#sponsor-tabs a.right').css('background-position', 'center 0');
    });
    
}
playground = function(){
    //div = document.createElement('div');
    //div.className = 'overlay';
    /*div = $('<div>').addClass('overlay');
    $('body').append(div).append(
        $('<div>').css({width: '100px', height: '100px',backgroundColor: "#FFF", position: 'absolute', top: '0', left:'0', zIndex:'100001'})
    );*/
    //var w = self.innerWidth || document.body.clientWidth;
    //w=$('body').outerWidth();
    //$("body","html").css({height: "100%", width: "100%"});
			//$("html").css("overflow","hidden");
    //var h = document.body.clientHeight;
    //$('<div />').css({width: w+'px', height: h+'px'}).addClass('overlay').appendTo('body');
    //showModal();
}


/* Gallery Related */
var ImagebaseURI= 'http://172.24.17.63/taha/megabucks/images/gallery/';
var numImages   = 5;
var captions    = ['Caption 1', 'Caption 2', 'Caption 3', 'Caption 4', 'Caption 5'];

var ImgPos      = 1;
var modalWidth  = 650;
var modalHeight = 500;



showModal = function(){
    var w = /*self.innerWidth  || */document.body.clientWidth;
    var h = /*self.innerHeight || */document.body.clientHeight;
    
    $('<div />').css({width: w+'px', height: h+'px'}).addClass('overlay').appendTo('body');
    $('<div />')
    .addClass('modal-box')
    .append($('<div />').addClass('modal-close-link'))
    .append($('<div />').addClass('modal-content')
            .append($('<div />').addClass('modal-content-img'))
            .append($('<div />').addClass('modal-left-button').addClass('t_png'))
            .append($('<div />').addClass('modal-right-button').addClass('t_png')))
    .append($('<div />').addClass('modal-caption'))
    .appendTo('body');
    if(jQuery.browser.msie){
        position();
        $('div.modal-left-button, div.modal-right-button').css('display', 'block');
        loadImage();
    }
    else{
        $('div.modal-box').animate({width: modalWidth+'px', height: modalHeight+'px', marginLeft:-(modalWidth/2)+'px', marginTop:-(modalHeight/2)+'px',border:'2px solid #888'},'slow','linear',showCallback);
    }
    
    registerEvents();
}

showCallback = function(){
    $('div.modal-left-button, div.modal-right-button').fadeIn('fast');
    loadImage();
}

registerEvents = function(){
    $(window).resize(position);
    
    $('.modal-left-button').bind('mouseenter', function(){
        if(ImgPos>1)
            $(this).css({backgroundPosition: '-77px center', cursor: 'pointer'});
    }).bind('mouseleave', function(){
        $(this).css({backgroundPosition: '0 center', cursor: 'default'});
    }).bind('click', function(){
        if(ImgPos>1)
            previousImage();
    });
    
    $('.modal-close-link').bind('click',closeModal);
    $('.overlay').bind('click',closeModal);
    
    $('.modal-right-button').bind('mouseenter', function(){
        if(ImgPos<numImages)
            $(this).css({backgroundPosition: '-77px center', cursor: 'pointer'});
    }).bind('mouseleave', function(){
        $(this).css({backgroundPosition: '0 center', cursor: 'default'});
    }).bind('click', function(){
        if(ImgPos<numImages)
            nextImage();
    });
}

position = function(){
    var w = /*self.innerWidth  || */document.body.clientWidth;
    var h = /*self.innerHeight || */document.body.clientHeight;
    
    $('div.overlay').css({width: w+'px', height: h+'px'});
    $('div.modal-box').css({width: modalWidth+'px', height: modalHeight+'px', marginLeft:-(modalWidth/2)+'px', marginTop:-(modalHeight/2)+'px',border:'2px solid #888'});
}

loadImage = function(){
    if(jQuery.browser.msie){
        $('div.modal-content-img').css('background-image', 'url('+imgAddr(ImgPos)+')');
        $('div.modal-caption').text(captions[ImgPos-1]);
    }
    else{
        $('div.modal-content').css('background-image', 'url(style/img/loading.gif)');
        $('div.modal-caption, div.modal-content-img').fadeOut('fast', function(){
            var Img = new Image();
            Img.src = imgAddr(ImgPos);
            //$('div.modal-content-img').css('background-image', 'url(style/img/loading.gif)');
            $(Img).load(function(){
                $('div.modal-content').css('background-image', 'none');
                $('div.modal-content-img').css('background-image', 'url('+imgAddr(ImgPos)+')').fadeIn('fast');
                $('div.modal-caption').text(captions[ImgPos-1]).fadeIn('fast');
            });
        });
        
    }
}

nextImage = function(){
    ImgPos++;
    checkPointer();
    loadImage();
}

previousImage = function(){
    ImgPos--;
    checkPointer();
    loadImage();
}

checkPointer = function(){
    if(ImgPos<=1)
        $('.modal-left-button').css({backgroundPosition: '0 center', cursor: 'default'});
    if(ImgPos>=numImages)
        $('.modal-right-button').css({backgroundPosition: '0 center', cursor: 'default'});
}

imgAddr = function(imgNo){
    return ImagebaseURI+imgNo+'.jpg';
}

closeModal = function(){
    if(jQuery.browser.msie){
        $('div.modal-box').remove();
        $('div.overlay').remove();
    }
    else{
        $('div.modal-content, div.modal-caption').fadeOut('fast',function(){
            $('div.modal-box').animate({width: 0, height: 0, marginLeft:0, marginTop:0,border:'none'},'slow','linear',function(){
                $('div.modal-box').remove();
                $('div.overlay').remove();
            });
        });
    }
}

//For highlighting Active Page in NavBar
//By ABHISHEK JAIN - DaDevilRocks
function extractPageName(hrefString)
{
	var arr = hrefString.split('/');
	return  (arr.length<2) ? hrefString : arr[arr.length-2].toLowerCase() + arr[arr.length-1].toLowerCase();               
}

function setActiveMenu(arr, crtPage)
{
	if(crtPage == "megabucksmarketing_contacts.php"){
		arr[5].className = "current";
		arr[5].parentNode.className = "current";
	}
	if((crtPage == "megabuckshospicontact.php") || (crtPage == "megabuckseventcordi.php") || (crtPage == "megabuckswebteam.php")){
		arr[7].className = "current";
		arr[7].parentNode.className = "current";
	}if((crtPage == "megabucksmedia08.php") || (crtPage == "megabucksmedianews.php")){
		arr[6].className = "current";
		arr[6].parentNode.className = "current";
	}
	for (var i=0; i<arr.length; i++)
	{
		if(extractPageName(arr[i].href) == crtPage)
		{
			if (arr[i].parentNode.tagName != "DIV")
			{
				arr[i].className = "current";
				arr[i].parentNode.className = "current";
			}
		}
	}
}

function setPage()
{
	hrefString = document.location.href ? document.location.href : document.location;

	if (document.getElementById("nav")!=null)
		setActiveMenu(document.getElementById("nav").getElementsByTagName("a"), extractPageName(hrefString));
}
/*function setpage()
{
	arr=document.getElementById("nav").getElementsByTagName("a");
	if (document.getElementById("nav")!=null)
	{
		for (var i=0; i<arr.length; i++)
		{
			if(arr[i].href == document.location)
			{
					arr[i].className = "current";
					arr[i].parentNode.className = "current";
			}
		}
	}
}*/

//For highlight in admad
function active()
{	
	arr=document.getElementById("links").getElementsByTagName("a");
	if (document.getElementById("links")!=null)
	{
		for (var i=0; i<arr.length; i++)
		{
			if(arr[i].href == document.location)
			{
					arr[i].className = "active";
					//arr[i].parentNode.className = "active";
			}
		}
	}
}