// JScript source code
	function openWtagWindow() {
		parent.MochaUI.wtagWindow();
	}
	function moveAdRankings() {

	    var x = $g.getBrowserWidth();
	    $$('div.mocha').each(function(windowEl){
	        if ($(windowEl).id == 'adRankings') {
	            var cascadeMorph = new Fx.Morph(windowEl, {
	                'duration': 550
	            });
				
				z = 340;
				
				z = x - parseInt(z);
	            cascadeMorph.start({
	                'top': 107,
	                'left': z
	            });
	        }
	        if ($(windowEl).id == 'wtagWindow') {
	            var cascadeMorph = new Fx.Morph(windowEl, {
	                'duration': 550
	            });
	            cascadeMorph.start({
	                'top': 107,
	                'left': x - 560
	            });
	        }
	        if ($(windowEl).id == 'shoutMixWindow') {
	            var cascadeMorph = new Fx.Morph(windowEl, {
	                'duration': 550
	            });
	            cascadeMorph.start({
	                'top': 420,
	                'left': x - 390
	            });
	        }
	    });
	}

    var global = {
        initY: 15,
        initX: 20,
        windowCounter: 0,
        itemHits: []
    };

    MochaUI.adWindow = function(itemId, subject){
        global.windowCounter++;
        new MochaUI.Window({
            id: 'adWindow_' + itemId,
            title: subject,
            closable: false,
            resizable: true,
            maximizable: true,
            closable: true,
            y: global.initY += ((global.windowCounter > 0)? 5 : 0),
            x: global.initX += ((global.windowCounter > 0)? 5 : 0),
            loadMethod: 'iframe',
            contentURL: 'display_ad.php?item_id='+itemId,
            scrollbars: true,
            padding: { top: 10, right: 10, bottom: 10, left: 10 },
            width: 1000,
            height: 470,
            onCloseComplete: function() {
                global.initY -= ((global.windowCounter > 0)? 5 : 0);
                global.initX -= ((global.windowCounter > 0)? 5 : 0);
                global.windowCounter -= 1;
            }
        });
    };

    MochaUI.newAdWindow = function(){
        new MochaUI.Window({
            id: 'newAdWindow',
            title: 'ePinoyBiz.com: New Ad',
            closable: true,
            resizable: false,
            maximizable: true,
            y: global.initY += ((global.windowCounter > 0)? 5 : 0),
            x: global.initX += ((global.windowCounter > 0)? 5 : 0),
            loadMethod: 'iframe',
            contentURL: 'new_ad.php',
            scrollbars: true,
            padding: { top: 10, right: 10, bottom: 10, left: 10 },
            width: 970,
            height: 609
        });
    };

    MochaUI.editAdWindow = function(itemId){
        new MochaUI.Window({
            id: 'editAdWindow_'+itemId,
            title: 'ePinoyBiz.com Manage Own Ads',
            type: 'window',
            closable: true,
            y: global.initY += ((global.windowCounter > 0)? 5 : 0),
            x: global.initX += ((global.windowCounter > 0)? 5 : 0),
            loadMethod: 'iframe',
            contentURL: itemId != undefined? 'own.php?item_id='+itemId : 'own.php',
            scrollbars: true,
            padding: { top: 10, right: 10, bottom: 10, left: 10 },
            width: 940,
            height: 609
        });
    };

    MochaUI.registerWindow = function(){
        new MochaUI.Window({
            id: 'registerWindow',
            title: 'Register@ePinoyBiz.com',
            type: 'window',
            minimizable: false,
            closable: true,
            resizable: false,
            maximizable: false,
            draggable: true,
            loadMethod: 'iframe',
            contentURL: 'register.php',
            scrollbars: true,
            padding: { top: 10, right: 10, bottom: 10, left: 10 },
            width: 480,
            height: 355,
            onContentLoaded: function() {
                if (parent.$('linkCloakingServiceWindow')) {
                    MochaUI.closeWindow($('linkCloakingServiceWindow'));
                }
            }
        });
    };

    MochaUI.loginWindow = function(uid){
        new MochaUI.Window({
            id: 'loginWindow',
            title: 'login@ePinoyBiz.com',
            type: 'window',
            minimizable: false,
            closable: true,
            resizable: false,
            maximizable: false,
            draggable: false,
            loadMethod: 'iframe',
            contentURL: 'login.php?uid='+uid,
            scrollbars: true,
            padding: { top: 10, right: 10, bottom: 10, left: 10 },
            width: 270,
            height: 190
        });
    };

    MochaUI.signoutWindow = function(){
        new MochaUI.Window({
            id: 'signoutWindow',
            title: 'Signout ePinoyBiz.com',
            type: 'window',
            minimizable: false,
            closable: true,
            resizable: false,
            maximizable: false,
            draggable: false,
            loadMethod: 'iframe',
            contentURL: 'login.php?Logout=True',
            scrollbars: true,
            padding: { top: 10, right: 10, bottom: 10, left: 10 },
            width: 0,
            height: 0,
            x: -10,
            y: -10,
            onContentLoaded: function() {
				parent.$('connectionRequestLink').style.display = 'none';
                parent.$('postAdLinkCheck').style.display = 'none';
                parent.$('editAdLinkCheck').style.display = 'none';
				parent.$('noticesLinkCheck').style.display = 'none';
				parent.$('networksLinkCheck').style.display = 'none';
                parent.$('welcomeLabel').style.display = 'none';
                parent.$('signoutLinkCheck').style.display = 'none';
                parent.$('registerLinkCheck').style.display = '';
                parent.$('registerLinkCheck').innerHTML = 'Register';
                parent.$('loginLinkCheck').style.display = '';
                parent.$('loginLinkCheck').innerHTML = 'Login';

				parent.$('userLogin').value = '';
				parent.$('userLoginId').value = '';

                if (parent.$('userLoginTd')) {
                	parent.$('userLoginTd').innerHTML = "I am looking for ";
                }
                MochaUI.notification('Logged out successfully ...');
                MochaUI.closeWindow($('signoutWindow'));
            }
        });
    }

    MochaUI.adRankingsWindow = function(){
        new MochaUI.Window({
            id: 'adRankings',
            closable: false,
            title: 'Classified Ad Rankings',
            loadMethod: 'iframe',
            contentURL: 'popular.php',
            width: 300,
            height: 430,
            x: 630,
            y: 100,
            resizeLimit: {'x': [300, 2500], 'y': [430, 2000]},
            toolbar: true,
            toolbarURL: '../mocha_v.0.9.5/pages/adrankings-tabs.html',
            contentBgColor: '#ffffff'
        });
    }

    MochaUI.shoutMixWindow = function(){
        new MochaUI.Window({
            id: 'shoutMixWindow',
            title: 'Shout',
            type: 'window',
            minimizable: false,
            closable: true,
            resizable: true,
            maximizable: false,
            draggable: true,
            loadMethod: 'iframe',
            contentURL: '../shoutMix/index.html',
            scrollbars: true,
            padding: { top: 10, right: 10, bottom: 10, left: 10 },
            width: 360,
            height: 175
        });
    };

    MochaUI.wtagWindow = function(){
		var ui = $('userId').value;
		var param = '';
		if (ui != undefined) {
			param = 'ms='+new Date()+'&ui='+ui;
		} else {
			param = 'ms='+new Date();
		}
        new MochaUI.Window({
            id: 'wtagWindow',
            title: 'Wall',
            type: 'window',
            minimizable: false,
            closable: false,
            resizable: false,
            maximizable: false,
            draggable: true,
            loadMethod: 'iframe',
            contentURL: 'wtag.php?'+param,
            scrollbars: true,
            padding: { top: 10, right: 10, bottom: 10, left: 10 },
            width: 211,
            height: 460
        });
    };

    MochaUI.searchWindow = function(){
        new MochaUI.Window({
            id: 'searchWindow',
            title: 'Search',
            type: 'window',
            minimizable: false,
            closable: true,
            resizable: true,
            maximizable: false,
            draggable: true,
            loadMethod: 'iframe',
            contentURL: 'search.php',
            scrollbars: true,
            padding: { top: 10, right: 10, bottom: 10, left: 10 },
            y: global.initY += ((global.windowCounter > 0)? 5 : 0),
            x: global.initX += ((global.windowCounter > 0)? 5 : 0),
            width: 990,
            height: 570
        });
    };

    MochaUI.connectionRequestWindow = function(unm,to_uid,uLogin){
        new MochaUI.Window({
            id: 'connectionRequestWindow',
            title: 'Connection Request',
            type: 'window',
            minimizable: false,
            closable: true,
            resizable: true,
            maximizable: false,
            draggable: true,
            loadMethod: 'iframe',
            contentURL: 'networks.php?u='+unm+'&to_uid='+to_uid+'&uLogin='+uLogin,
            scrollbars: true,
            padding: { top: 10, right: 10, bottom: 10, left: 10 },
            y: global.initY += ((global.windowCounter > 0)? 5 : 0),
            x: global.initX += ((global.windowCounter > 0)? 5 : 0),
            width: 475,
            height: 164
        });
    };

    MochaUI.pendingConnectionRequestsWindow = function(unm,uid){
        new MochaUI.Window({
            id: 'pendingConnectionRequestsWindow',
            title: 'Connection Requests List',
            type: 'window',
            minimizable: false,
            closable: true,
            resizable: true,
            maximizable: false,
            draggable: true,
            loadMethod: 'iframe',
            contentURL: 'networks.php?u0='+unm+'&uid='+uid,
            scrollbars: true,
            padding: { top: 10, right: 10, bottom: 10, left: 10 },
            y: global.initY += ((global.windowCounter > 0)? 5 : 0),
            x: global.initX += ((global.windowCounter > 0)? 5 : 0),
            width: 730,
            height: 530
        });
    };

    MochaUI.networksWindow = function(unm,uid){
        new MochaUI.Window({
            id: 'networksWindow',
            title: 'List of Networks',
            type: 'window',
            minimizable: false,
            closable: true,
            resizable: true,
            maximizable: false,
            draggable: true,
            loadMethod: 'iframe',
            contentURL: 'networks.php?u0='+unm+'&uid='+uid+'&uLoginId=1',
            scrollbars: true,
            padding: { top: 10, right: 10, bottom: 10, left: 10 },
            y: global.initY += ((global.windowCounter > 0)? 5 : 0),
            x: global.initX += ((global.windowCounter > 0)? 5 : 0),
            width: 730,
            height: 530
        });
    };

    MochaUI.settingsWindow = function(){
        new MochaUI.Window({
            id: 'settingsWindow',
            title: 'eStore Settings',
            type: 'window',
            minimizable: false,
            closable: true,
            resizable: true,
            maximizable: false,
            draggable: true,
            loadMethod: 'iframe',
            contentURL: 'settings.php',
            scrollbars: true,
            padding: { top: 10, right: 10, bottom: 10, left: 10 },
            y: global.initY += ((global.windowCounter > 0)? 5 : 0),
            x: global.initX += ((global.windowCounter > 0)? 5 : 0),
            width: 470,
            height: 265
        });
    };

    MochaUI.linkCloakingServiceWindow = function(){
        new MochaUI.Window({
            id: 'linkCloakingServiceWindow',
            title: 'Link Cloaking/Masking Service',
            type: 'window',
            minimizable: false,
            closable: true,
            resizable: true,
            maximizable: false,
            draggable: true,
            loadMethod: 'xhr',
            contentURL: 'linkCloakingAd.html',
            scrollbars: true,
            padding: { top: 10, right: 10, bottom: 10, left: 10 },
            y: global.initY += ((global.windowCounter > 0)? 5 : 0),
            x: global.initX += ((global.windowCounter > 0)? 5 : 0),
            width: 470,
            height: 375
        });
    };

    window.addEvent('domready', function(){
        var i = 1;

        // traverse through grid data
        while ($('directory_itemsjobAdsSubject_' + i)) {

            // event bindiing each ad subject
            $('directory_itemsjobAdsSubject_' + i).addEvent('click', function(e){
                new Event(e).stop();
                var rowNum = this.id.substring(this.id.lastIndexOf('_')+1);
                if (location.href != top.location.href) {
                    parent.MochaUI.adWindow(
                        $('directory_itemsitemId_' + rowNum).value, rowNum,
                        $('directory_itemsjobAdsSubTxt_' + rowNum).value);

                } else {
                    MochaUI.adWindow(
                        $('directory_itemsitemId_' + rowNum).value,
                        $('directory_itemsjobAdsSubTxt_' + rowNum).value
                                        );
                }
            });

            // event bindiing each edit ad link
            $('directory_itemseditAdLink_' + i).addEvent('click', function(e){
                new Event(e).stop();
                var rowNum = this.id.substring(this.id.lastIndexOf('_')+1);
                if (location.href != top.location.href) {
                    parent.MochaUI.editAdWindow(
                        $('directory_itemsitemId_' + rowNum).value, rowNum,
                        $('directory_itemseditAdLink_' + rowNum).value
                                        );
                } else {
                    MochaUI.editAdWindow(
                        $('directory_itemsitemId_' + rowNum).value,
                        $('directory_itemseditAdLink_' + rowNum).value
                                        );
                }
            });

            i++;
        };

        var i = 1;

        // traverse through grid data
        while ($('yp_griditem_name_' + i)) {

            // event bindiing each ad subject
            $('yp_griditem_name_' + i).addEvent('click', function(e){
                new Event(e).stop();
                var rowNum = this.id.substring(this.id.lastIndexOf('_')+1);
                if (location.href != top.location.href) {
                    parent.MochaUI.adWindow(
                        $('yp_griditemId_' + rowNum).value, rowNum,
                        $('yp_gridadSubject_' + rowNum).value
                                        );
                } else {
                    MochaUI.adWindow(
                        $('yp_griditemId_' + rowNum).value,
                        $('yp_gridadSubject_' + rowNum).value
                                        );
                }
            });
            i++;
        };

        $('postAdLinkCheck').addEvent('click', function() {
            if (location.href != top.location.href) {
                parent.MochaUI.newAdWindow();
            } else {
                MochaUI.newAdWindow();
            }
        });

        $('editAdLinkCheck').addEvent('click', function() {
            if (location.href != top.location.href) {
                parent.MochaUI.editAdWindow();
            } else {
                MochaUI.editAdWindow();
            }
        });

        if ($('registerLinkCheck')){
            $('registerLinkCheck').addEvent('click', function(e){
                MochaUI.registerWindow();
            });
        }

        if ($('loginLinkCheck')){
            $('loginLinkCheck').addEvent('click', function(e){
                MochaUI.loginWindow($('userId').value);
            });
        }

        if ($('signoutLinkCheck')){
            $('signoutLinkCheck').addEvent('click', function(e){
                new Event(e).stop();
                MochaUI.signoutWindow();
            });
        }

        if ($('Path_count_per_categorysearchLink')){
            $('Path_count_per_categorysearchLink').addEvent('click', function(e){
                new Event(e).stop();
                MochaUI.searchWindow();
            });
        }

        if ($('searchLink')){
            $('searchLink').addEvent('click', function(e){
                new Event(e).stop();
                MochaUI.searchWindow();
            });
        }

        if ($('connectionRequestLink')){
            $('connectionRequestLink').addEvent('click', function(e){
                new Event(e).stop();
                //alert('$(\'userLogin\').value: '+$('userLogin').value);
				MochaUI.connectionRequestWindow($('userName').value,$('userId').value,$('userLogin').value);
            });
        }

		if ($('noticesLinkCheck')) {
			$('noticesLinkCheck').addEvent('click', function(e) {
				new Event(e).stop();
				MochaUI.pendingConnectionRequestsWindow($('userName').value,$('userId').value);
			});
		}

		if ($('networksLinkCheck')) {
			$('networksLinkCheck').addEvent('click', function(e) {
				new Event(e).stop();
				MochaUI.networksWindow($('userName').value,$('userId').value);
			});
		}

        if ($('bannerAds')){
            $('bannerAds').addEvent('click', function(e){
                MochaUI.registerWindow();
            });
        }

        if ($('userLoginLink')){
            $('userLoginLink').addEvent('click', function(e){
				new Event(e).stop();
				MochaUI.settingsWindow();
            });
        }

        /*if ($('freeLinkCloaker')){
            $('freeLinkCloaker').addEvent('click', function(e){
				new Event(e).stop();
				MochaUI.registerWindow();
            });
        }*/

		MochaUI.adRankingsWindow();
		moveAdRankings();
        //MochaUI.wtagWindow();

        // traverse through member's gallery
        var i = 1;
        while ($('directory_items1RowComponentsjob_ads_subject_' + i)) {

            // event binding each ad subject
            $('directory_items1RowComponentsjob_ads_subject_' + i).addEvent('click', function(e){
                new Event(e).stop();
                var rowNum = this.id.substring(this.id.lastIndexOf('_')+1);
                var subject = $('directory_items1RowComponentsjob_ads_subject_' + rowNum).innerHTML;
                if (location.href != top.location.href) {
                    parent.MochaUI.adWindow(
                        $('directory_items1RowComponentsitem_id_' + rowNum).value, rowNum,
                        subject);
                } else {
                    MochaUI.adWindow(
                        $('directory_items1RowComponentsitem_id_' + rowNum).value,
                        subject);
                }
            });

            if ($('directory_items1RowComponentsad_image_' + i)) {
                $('directory_items1RowComponentsad_image_' + i).addEvent('click', function(e){
                    new Event(e).stop();
                    var rowNum = this.id.substring(this.id.lastIndexOf('_')+1);
                    var subject = $('directory_items1RowComponentsjob_ads_subject_' + rowNum).innerHTML;
                    if (location.href != top.location.href) {
                        parent.MochaUI.adWindow(
                            $('directory_items1RowComponentsitem_id_' + rowNum).value, rowNum,
                            subject);
                    } else {
                        MochaUI.adWindow(
                            $('directory_items1RowComponentsitem_id_' + rowNum).value,
                            subject
                                                );
                    }
                });
            }

            i++;
        };

        // traverse through member's featured gallery
        var i = 1;
        while ($('directory_items2RowComponentsjob_ads_subject_' + i)) {

            // event binding each ad subject
            $('directory_items2RowComponentsjob_ads_subject_' + i).addEvent('click', function(e){
                new Event(e).stop();
                var rowNum = this.id.substring(this.id.lastIndexOf('_')+1);
                var subject = $('directory_items2RowComponentsjob_ads_subject_' + rowNum).innerHTML;
                if (location.href != top.location.href) {
                    parent.MochaUI.adWindow(
                        $('directory_items2RowComponentsitem_id_' + rowNum).value, rowNum,
                        subject);
                } else {
                    MochaUI.adWindow(
                        $('directory_items2RowComponentsitem_id_' + rowNum).value,
                        subject);
                }
            });

            if ($('directory_items2RowComponentsad_image_' + i)) {
                $('directory_items2RowComponentsad_image_' + i).addEvent('click', function(e){
                    new Event(e).stop();
                    var rowNum = this.id.substring(this.id.lastIndexOf('_')+1);
                    var subject = $('directory_items2RowComponentsjob_ads_subject_' + rowNum).innerHTML;
                    if (location.href != top.location.href) {
                        parent.MochaUI.adWindow(
                            $('directory_items2RowComponentsitem_id_' + rowNum).value, rowNum,
                            subject);
                    } else {
                        MochaUI.adWindow(
                            $('directory_items2RowComponentsitem_id_' + rowNum).value,
                            subject
                                                );
                    }
                });
            }

            i++;
        };

    });

    function loadPolling(elemId, url, milliSec, parameters) {
        intervalVar = setInterval(
        function() {
            new Request.HTML({
                method: 'get',
                url: url,
                data: parameters,
                onRequest: function() {
                    //alert('Request made. Please wait...');
                },
                onComplete: function(response) {
                    var updateItem = function(element, index, array) {
                        if ($('numHits_' + element.item_id)) {
                            $('numHits_' + element.item_id).innerHTML = element.hits + ' hits';
                        }
                    };
                    var updateItems = function(arr) {
                    	arr.forEach(updateItem);
                    };
                    global.itemHits = json_parse(response[0].data);
                    updateItems(global.itemHits);
                }
            }).send()
        },
        milliSec
        );
    }