/**
 SITE JAVASCRIPT 
/**/

/** SETTING FOR POP_UP /**/
var popup = {
    close: function() {
        $('.overlay').addClass('hidden');
        $('.popup > .body > .display').html('');
    },
    setTitle: function(title) {
        //	alert("Setting title to " + title);
        $('.popup > .body > .title').html(title);
    },
    setDescription: function(description) {
        //	alert('Setting descriptin to ' + description);
        $('.popup >.body > .description').html(description);
    },
    setEmbedCode: function(embed) {
        //	alert('Setting embedcode');
        $('.popup > .body > .display').html(embed);
    },
    center: function() {
        $('html, body').animate({ scrollTop: 0 }, 'slow');
    }

};

var work = {
    showVideo: function(id) {
        /** @TODO Ajax call to obtain the embed code title and description /**/
        //	alert('hit');
        video.showVideo(id);
    },
    showOurWork: function(id) {
        $.ajax({
            url: "/OurWork/ShowWork/" + id,
            dataType: "json",
            cache: false,
            success: function(data) {
                popup.center();
                popup.setTitle(data.title);
                popup.setDescription(data.description);
                popup.setEmbedCode("<a href='" + data.link + "'>View Work</a>");
                $('.overlay').removeClass('hidden');
            }
        });
    },
    navigate: function(url) {
        window.location = url;
    }
};

var video = {
    showVideo: function(id) {
        $.ajax(
	    { url: "/HomeFeatures/getVideoData/" + id,
	      dataType: "json",
	      cache: false,
	      success: function(data) {
	            popup.center();
	            popup.setTitle(data.overlay_title);
	            popup.setDescription(data.overlay_text);
	            popup.setEmbedCode(data.embed_code);
	            $('.overlay').removeClass('hidden');
	        }

	    });
    }
};

var ourTeam = {
    init: function() {
        /**@TODO: NEEDS TO READ THE URL IN ORDER OPEN DIRECTLY /**/
        $('.directory-item').click(
			function() {
			    ourTeam.getNavItem($(this));
			    return false;
			}
		);
        $('.tabs > ul > li > a').click(
			function() {
			    ourTeam.getTabItem($(this));
			    return false;
			}
		);
        ourTeam.pageLoad();
    },
    getNavItem: function(element) {
        var el = element.attr('href');
        $('.details').addClass('hidden');
        $(el).removeClass('hidden');
        $('.directory-item').parent('li').removeClass('current');
        element.parent('li').addClass('current');

    },
    getTabItem: function(element) {
        var el = element.attr('href');
        $('.tabs > ul > li').removeClass('current');
        element.parent('li').addClass('current');
        $('.section').addClass('hidden');
        $('.section' + el).removeClass('hidden');
        $(el + ' > div >ul> li > .directory-item:first').click();
		if(el == "#investors")
			$(el + ' > div.details').removeClass('hidden');
    },
    pageLoad: function() {
        url = window.location.toString();

        loc = url.indexOf('#');
        if (loc > 1) {
            navId = url.substr(loc);
            nav = $(navId);

            tabId = nav.parent('div').attr('id');
            tab = $('#' + tabId);

            ourTeam.getTabItem(tab);
            ourTeam.getNavItem(nav);
            $('a[href=' + navId + ']').parent('li').addClass('current');
            $('a[href=#' + tabId + ']').parent('li').addClass('current');

            tab.removeClass('hidden');
            nav.removeClass('hidden');
        } else {
            $('.tabs > ul > li:first > a ').click();
        }
    }
};

var Quotes = {
    data: null,
    cnt: 0,
    init: function(type) {
        Quotes.getQuote(type);
    },
    getQuote: function(type) {
        $.ajax({
            url: "/Quote/getQuote/?type=" + type,
            dataType: "json",
            success: function(json) {
                Quotes.data = json;
                setInterval("Quotes.process()", 20000);
            }
        });
    },
    process: function() {
        Quotes.cnt = Quotes.cnt * 1 + 1;

        if (Quotes.cnt % 5 == 0)
            Quotes.cnt = 0;

        $(".quote").html(Quotes.data[Quotes.cnt].quote);
        $(".name").html(Quotes.data[Quotes.cnt].source);
        $(".company").html(Quotes.data[Quotes.cnt].source_location);
    }
};


var ourNews = {
    init: function() {
        $('.tabs li div.center a').click(
			function() {
			    ourNews.getType($(this).attr('href'));
			    $('.tabs > li').removeClass('current');
			    $(this).parent().parent('li').addClass('current');
			    $('.scroll-pane').jScrollPane({ scrollbarWidth: 7 });
			    return false;
			}
		);


        $('.tabs li div.center a:first').click();
    },
    getType: function(type) {
        if (type == "#all") {
            //$('.item').removeClass('hidden');
            $('.item').parents('li').removeClass('hidden');
        } else {
            // $('.item').addClass('hidden');
            $('.item').parents('li').addClass('hidden');
            trueType = type.substr(1);
            // $('.' + trueType).removeClass('hidden');
            $('.' + trueType).parents('li').removeClass('hidden');
        }
    }
};

var OurWork = {
    init: function() {
        $('.tabs li div.center a').click(
			function() {
			    OurWork.getType($(this).attr('href'));
			    $('.tabs > li').removeClass('current');
			    $(this).parent().parent('li').addClass('current');
			    return false;
			}
		);
        OurWork.pageLoad();
    },
    getType: function(type) {
        if (type == "#all") {
            $('.work').removeClass('hidden');
        } else {
            $('.work').addClass('hidden');
            trueType = type.substr(1);
            $('.' + trueType).removeClass('hidden');
        }
    },
    showWork: function(id) {
        $.ajax({
            url: "/OurWork/ShowWork/" + id,
            dataType: "json",
            cache: false,
            success: function(data) {
                popup.center();
                popup.setTitle(data.title);
                popup.setDescription(data.description);
                popup.setEmbedCode(data.embed_code);
                $('.overlay').removeClass('hidden');
            }
        });
    },
    pageLoad: function() {
        url = window.location.toString();

        loc = url.indexOf('#');
        if (loc > 1) {
            navId = url.substr(loc);
            nav = $(navId + ' > div.center > a').click();
           // $(navId + ' > div.thumbnail > div.description').click();
        } else {
            $('.tabs > ul > li:first > a ').click();
        }
    }

};

var WhatWeDo = {
	enlarge:function(section){
		var image = $(section + ' > div.enlarge ').attr('large');
		popup.center();
        popup.setTitle('');
        popup.setDescription('');
        popup.setEmbedCode('<img src="'+image+'" width="649px" height="365px"/>');
        $('.overlay').removeClass('hidden');
	},/**
	showImage:function(img){
		$('.viewports > img.active').removeClass('active');
		$('.viewports > img').addClass('hidden');
		$(img).addClass('active');
		return false;
	},/**/
	showImage:function(section, item)
	{
		$(section + ' > div.viewports > img').removeClass('active');
		$(section + ' > div.viewports > img').addClass('hidden');
		$(section + ' > div.viewports > img' + item+':first').addClass('active').removeClass('hidden');
		img = $(section + ' > div.viewports > img' + item+':first').attr('large');
		$(section + ' > div.enlarge ').attr('large', img);
	/**	return false; /**/
	}
	
};

var ContactUs = {
	init:function() {
		/** This is where we define the actions to conduct /**/
		$('.offices').click(function(){
			ContactUs.list($(this));
		});
	},
	
	list:function(item){
		var pinid = item.children('span.location').attr('location');
		$('.offices').removeClass('current');
		$('.detail').addClass('hidden');
		$(item).addClass('current');
		$(item).children('div.detail').removeClass('hidden');
		$('.pin').removeClass('current');
		$('#' + pinid).addClass('current');

		$('.scroll-pane').jScrollPane({ scrollbarWidth: 7 });
	}
	
};


var Career = {
	
	show:function(details){
			content = $(details).html();
			popup.center();
			$('div.popup > div.body > div.display').attr('style','text-align:left');
	        popup.setTitle('');
	        popup.setDescription('');
	        popup.setEmbedCode(content);
	        $('.overlay').removeClass('hidden');
	}
};

var Tweets = {
    tweet: Array(2),
    count: 0,
    init: function() {
        $.ajax({
            url: '/home/tweets/',
            dataType: 'json',
            cache:false,
            success: function(json) {
                Tweets.tweet[0] = json.tweet1;
                Tweets.tweet[1] = json.tweet2;
                setInterval("Tweets.process()", 20000);
            }
        });

    },
    process: function() {
        Tweets.count = Tweets.count * 1 + 1;
        if (Tweets.count % 2 == 0) {
            if(Tweets.tweet[0].name != "NOT CONNECTED")
            Tweets.display(Tweets.tweet[0]);
        }
        else {
            if (Tweets.tweet[1].name != "NOT CONNECTED")
            Tweets.display(Tweets.tweet[1]);
        }
    },
    display: function(twt) {
        var tweet = 'div.get-social > div.module-content > div.tweet';
        var name = 'div.get-social > div.module-content > div.timestamp > div.green';
        var time = 'div.get-social > div.module-content > div.timestamp > div.grey';
        $(name).html(twt.name);
        $(tweet).html(twt.tweet);
        $(time).html(twt.time);
    }
};

