/*
* Copyright (c) Lukup Media Pvt Limited, India.
* All rights reserved.
*
* This software is the confidential and proprietary information of Lukup Media Pvt Limited ("Confidential Information").
* You shall not disclose such Confidential Information and shall use it only in accordance with the terms
* of the licence agreement you entered into with Lukup Media Pvt Limited.
*
*/

function GetXmlHttpObject() {
    if (window.XMLHttpRequest) {
        return new XMLHttpRequest();
    }else if(window.ActiveXObject) {
        return new ActiveXObject("Microsoft.XMLHTTP");
    } else{
        alert("Your browser doesn't support");
        return false;
    }
}

var xmlhttp = GetXmlHttpObject();
var widgetCount=0;
function showRandomWidget(no, place, page) {
    var randomWidgetDiv = document.getElementById(place);
  //  var url = "/portal/DevAction.do?method=getRandomDevAppList&noOfApp="+no;
  var url = "/portal/DevAction.do?method=getRandomTypeDevAppList";
    xmlhttp.onreadystatechange = function () {
        if (xmlhttp.readyState == 4 ) {
            var xmldoc = xmlhttp.responseXML;
            var x = xmldoc.getElementsByTagName("devAppList");
            var widgets = xmldoc.getElementsByTagName("widget");
			var size  = widgets.length;
            var classType;
			var fnlStr="";
			var newFnlStr="";

            if (size != 0) {
                for (var j = 0; j < size; j++) {
                    var widgetName = widgets[j].getElementsByTagName("widgetName")[0].firstChild.nodeValue;
                    var widgetDescription = widgets[j].getElementsByTagName("widgetDescription")[0].firstChild.nodeValue;
                    var widgetType = widgets[j].getElementsByTagName("widgetType")[0].firstChild.nodeValue;
                    var widgetCategory = widgets[j].getElementsByTagName("widgetCategory")[0].firstChild.nodeValue;
                    var widgetLogoPath = widgets[j].getElementsByTagName("widgetLogoPath")[0].firstChild.nodeValue;
                    var widgetAppid = widgets[j].getElementsByTagName("widgetAppid")[0].firstChild.nodeValue;
                    var widgetPlatformid = widgets[j].getElementsByTagName("widgetPlatformid")[0].firstChild.nodeValue;
					
					var producturl = "/portal/UserAction.do?method=showProduct&name=" + widgetName + "&appid=" + widgetAppid + "&platformid=" + widgetPlatformid;
					var textType;
					if(widgetDescription.length > 64) {
                        widgetDescription = widgetDescription.substring(0, 64)+'..';
                    }
					if(widgetType == 'Mobile'){  
						type = '<img src="ui/images/runon_mob.png" alt="Android | BlackBerry | Symbian OS" />';					
						classType="mobile";
						textType="a Mobile Application";
                    } else if(widgetType == 'Desktop'){    
						type = '<img src="ui/images/runon_desktop.png" alt="Desktop" />';					
						classType="desktop";
						textType="a Desktop Application";
                    }else if(widgetType == 'Web'){                        
						 type = '<img src="ui/images/runon_web.png" alt="Web" />';
						classType="online";
						textType="an Online Campaign";
					}else if(widgetType == 'Social Network'){
						 type = '<img src="ui/images/runon_web.png" alt="Web" />';
						classType="social";
						textType="a Social Campaign";
                    }else if(widgetType == 'DTH'){                       
						 type = '<img src="ui/images/runon_dth.png" alt="DTH" />';
						classType="dth";
						textType="a TV Campaign";
                    }else if(widgetType == 'IPTV'){                        
						type = '<img src="ui/images/runon_iptv.png" alt="IPTV" />';
						classType="iptv";
						textType="an IPTV Campaign";
                    };
				if(widgetType != 'Desktop'){
				widgetCount++;
					if(page=="home")
					{			
					newFnlStr+='<div class="type_box">'+
							'<ul class="clearfix">'+
							'<li class="leftli">'+
								'<span><img src="'+widgetLogoPath+'" alt="'+widgetName+'" /></span>'+
							'</li>'+
							'<li class="rightli">'+
										'<p class="wid_name">'+widgetName+'</p>'+
										'<p class="wid_desc">'+widgetDescription+'</p>'+								
									'</li>'+
									'<li class="wid_links">'+
										'<a class="more_link" href="javascript:void(0);" onclick="return getFeatures(\''+widgetType+'\');">Learn more on how to create '+textType+'</a>'+
									'</li>'+
									'<span class="type_tag_'+classType+'"></span>'+
							'</ul>'+
						'</div>';	
						}
						else{
					fnlStr += '<li class="feaApp">'+
					'<div class="appsBlock clearfix">'+
						'<ul class="overview clearfix">'+
							'<li class="prdImg"><a href="'+producturl+'"><img src="'+widgetLogoPath+'" alt="'+widgetName+'" /></a></li>'+
							'<li class="prdDesc">'+
								'<h3>'+widgetName+'</h3>'+
								'<p>'+widgetDescription+'</p>'+
							'</li>'+
							'<li class="runsOn">'+
								'<h2>Runs on</h2>'+
								'<p>'+type+'</p>'+
							'</li>'+
						'</ul>'+						
					'</div>'+
					'</li>';
					}
				}// end if:desktop
				}
            }
        	if(newFnlStr != ""  && page=="home") {
                randomWidgetDiv.innerHTML = newFnlStr;
            } 
			if(fnlStr != "" && page!="home" ) {
                randomWidgetDiv.innerHTML =fnlStr;
            } 
								
				$('#widgetHolder .type_box:first').addClass("activeBox");	
				$('.fea_arw_left').click(function(){
						if($(".type_box").index($(".activeBox"))==0)
						{
						$('#widgetHolder .activeBox').fadeOut(1000).removeClass("activeBox");
						$('#widgetHolder .type_box:last').fadeIn(1000).addClass("activeBox");
						}
						else{
						$('#widgetHolder .activeBox').removeClass("activeBox").fadeOut(1000).prev().fadeIn(1000).addClass("activeBox");
						}

				});
				$('.fea_arw_right').click(function(){
					rightArrow();
				});
			
        }
    };
	xmlhttp.open("GET", url, true);
    xmlhttp.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT");
    xmlhttp.send(null);
}
function rightArrow(){
		if($(".type_box").index($(".activeBox"))==(widgetCount-1))
		{
			$('#widgetHolder .activeBox').fadeOut(1000).removeClass("activeBox");
			$('#widgetHolder .type_box:first').fadeIn(1000).addClass("activeBox");
		}
		else{
			$('#widgetHolder .activeBox').removeClass("activeBox").fadeOut(1000).next().fadeIn(1000).addClass("activeBox");
		}
}
function getFeatures(widgetType)
{
	document.forms[0].action = "/portal/UserAction.do?method=getFeatures&type="+widgetType;
    document.forms[0].submit();
	return false;
}

