irise.namespace("irise"); // sets the namespace
irise.iPhoneTabBarVertical = function(){
}

irise.iPhoneTabBarVertical.prototype.onload = function(){
	var widgetWidth = this.getProperty('width');
	this.getWidgetDiv().width(widgetWidth);
	var widgetHeight = this.getProperty('height');
	this.getWidgetDiv().height(widgetHeight);
	// shortening large identifiers
	var forDivId = this.getId();
	var tBVJSId = this.getId();
	var me = this;
	var customIcon = true;
	var standeredIcon = true;
	tBVJSId = tBVJSId.substring(tBVJSId.length-4);
	var prevDivSelected = tbSelectedTab;
	// checking for mobile or not
	var tabBarTagertWidget = this.getProperty('tbTargtWidget');
	if(tabBarTagertWidget == "" || tabBarTagertWidget == null || tabBarTagertWidget == "undefined"){
		tabBarTagertWidget = "";	// no need to check the autoshow field
	}
	var tbarLabeles = this.getProperty('tbLabels');
	var tbHideLabel = this.getProperty('tbHideLabel');
	var tbSelectedTab = this.getProperty('tbSelectedTab');
	if(jQuery.inArray(tbSelectedTab,tbarLabeles)<0)
		tbSelectedTab = tbarLabeles[0];
	var tbStandardIcons = this.getProperty('tbStandardIcons');
	if(typeof(tbStandardIcons) =="string")
	{
		var temptbStandardIcons = tbStandardIcons;
		tbStandardIcons = new Array();
		tbStandardIcons[0] = temptbStandardIcons;
	}
	if(tbStandardIcons == "" || tbStandardIcons == null || tbStandardIcons == "undefined")
	{
		tbStandardIcons = new Array();
		tbStandardIcons[0] = "";
		 //standeredIcon = false;
	}
	var currentSelectedTab = (jQuery.inArray(tbSelectedTab,tbarLabeles))+1;
	var prevDivSelected ="innerDivId"+currentSelectedTab;
	var bgImg = this.getProperty('tbImagePicker');
	var TBVJSlabelColor = this.getProperty('tabBarFontColor');
	if(TBVJSlabelColor == "" || TBVJSlabelColor == null || TBVJSlabelColor == "undefined")
		TBVJSlabelColor = "#999999";
	var tbCustomIcons = this.getProperty('tbCustomImages');
	var default_hlit = this.getProperty('default_highlight');
	var selectedLabelColor = this.getProperty('tbSelectedFontColor');
	if(selectedLabelColor == "" || selectedLabelColor == null || selectedLabelColor == "undefined")
		 selectedLabelColor = "#FFFFFF";
	if(typeof(tbCustomIcons) =="string")
	{
		var temptbCustomIcons = tbCustomIcons;
		tbCustomIcons = new Array();
		tbCustomIcons[0] = temptbCustomIcons;
	}
	if(tbCustomIcons == "" || tbCustomIcons == null || tbCustomIcons == "undefined")
	{
		 tbCustomIcons = new Array();
		tbCustomIcons[0] = "";
		//customIcon = false;
	}
	var tbCustomSelectedIcons = this.getProperty('tbCustomSelectedImages');
	if(typeof(tbCustomSelectedIcons) =="string")
	{
		var temptbCustomSelectedIcons = tbCustomSelectedIcons;
		tbCustomSelectedIcons = new Array();
		tbCustomSelectedIcons[0] = temptbCustomSelectedIcons;
	}
	if(tbCustomSelectedIcons == "" || tbCustomSelectedIcons == null || tbCustomSelectedIcons == "undefined")
	{
		tbCustomSelectedIcons = new Array();
		tbCustomSelectedIcons[0] = "";
	}
	
	
	var selectedDevice = this.getProperty('tbDeviceSelect');
	if(selectedDevice == "" || selectedDevice == null || selectedDevice == "undefined")
		 selectedDevice = "iPhone Portrait";
	switch(selectedDevice)
	{
		case "iPhone Landscape":
			widgetWidth = 480;
		break;
		case "iPad Portrait":
			widgetWidth = 768;
		break;
		case "iPad Landscape":
			widgetWidth = 1024;
		break;
		case "Custom Width":
			widgetWidth = this.getProperty('width');
		break;
		default:
			widgetWidth = 320;
		break;
	}
	if(!bgImg)
	{
		widgetHeight = 49;
		//widgetWidth = 320;
	}
	if((tbCustomIcons == "" || tbCustomIcons == null || tbCustomIcons == "undefined") && (tbCustomSelectedIcons == "" || tbCustomSelectedIcons == null || tbCustomSelectedIcons == "undefined")){
		customIcon = false;
	}
	
	var toAppendDiv = ' <div class="containerDivTBVJS" id="container'+tBVJSId+'"><div class = "innerClassTBVJS" id="inner'+tBVJSId+'"></div></div>';
	// add the widget to the page
	this.getWidgetDiv().append(toAppendDiv);
	$('#container'+tBVJSId).css('height',widgetHeight+'px');
	$('#container'+tBVJSId).css('width',widgetWidth+'px');
	if(bgImg)
		$('#'+forDivId+' .containerDivTBVJS').css('background-image', 'url('+bgImg+')');
	var innerdivs ="";
	var innerDivWidth=Math.floor((widgetWidth)/(tbarLabeles.length));
	
	var textLabel;
	for(var i =1;i<=tbarLabeles.length; i++)
	{
		textLabel = tbarLabeles[i-1];
		if(textLabel == null || textLabel=="")
			textLabel = "";
		//innerdivs = innerdivs+'<div id="'+tBVJSId+'innerOuterContainer'+i+'"><div id="'+tBVJSId+'innerContentDivIdLeft'+i+'"></div><div id="'+tBVJSId+'innerContentDivId'+i+'"><div class="TBVJSinnerclass'+i+'" id="'+tBVJSId+'innerDivId'+i+'"><span class="TBVJSinnerTextClass">'+textLabel+'</span></div></div><div id="'+tBVJSId+'innerContentDivIdRight'+i+'"></div></div>';
		innerdivs = innerdivs+'<div id="'+tBVJSId+'innerOuterContainer'+i+'"><div id="'+tBVJSId+'innerContentDivId'+i+'"><div class="TBVJSinnerclass'+i+'" id="'+tBVJSId+'innerDivId'+i+'"><span class="TBVJSinnerTextClass">'+textLabel+'</span></div></div></div>';
	}
	$('#'+forDivId+' .innerClassTBVJS').append(innerdivs);
	if(tbHideLabel)
			$('#'+forDivId+' .TBVJSinnerTextClass').css('visibility','hidden');	
	for(var i =1;i<=tbarLabeles.length; i++)
	{
		$('#'+tBVJSId+'innerOuterContainer'+i).css('width',innerDivWidth+'px');
		$('#'+tBVJSId+'innerOuterContainer'+i).css('float','left');
		//$('#'+tBVJSId+'innerContentDivId'+i).css('width',innerDivWidth-14+'px');
		$('#'+tBVJSId+'innerContentDivId'+i).css('width',innerDivWidth+'px');
		$('#'+tBVJSId+'innerContentDivId'+i).css('float','left');
		$('#'+tBVJSId+'innerContentDivIdLeft'+i).css('width','7px');
		$('#'+tBVJSId+'innerContentDivIdLeft'+i).css('float','left');
		$('#'+tBVJSId+'innerContentDivIdLeft'+i).css('height','49px');
		$('#'+tBVJSId+'innerContentDivIdRight'+i).css('width','7px');
		$('#'+tBVJSId+'innerContentDivIdRight'+i).css('float','left');
		//$('#'+tBVJSId+'innerDivId'+i).css('width',innerDivWidth-14+'px');
		$('#'+tBVJSId+'innerDivId'+i).css('width',innerDivWidth-4+'px');
		$('#'+tBVJSId+'innerDivId'+i).css('height',widgetHeight+'px');
		$('#'+tBVJSId+'innerDivId'+i).css('color',TBVJSlabelColor);
		$('#'+tBVJSId+'innerDivId'+i).css('padding-top',widgetHeight-11+'px');
		if($.browser.msie)
				$('#'+tBVJSId+'innerDivId'+i).css('padding-top',widgetHeight-9+'px');
		if($.browser.opera)
				$('#'+tBVJSId+'innerDivId'+i).css('padding-top',widgetHeight-13+'px');
		if(customIcon)
		{
			$('#'+tBVJSId+'innerDivId'+i).css('height',widgetHeight+'px');
			$('#'+tBVJSId+'innerDivId'+i).css('background-position','50% 30%');
			if(tbCustomIcons[i-1])
				$('#'+tBVJSId+'innerDivId'+i).css('background-image', 'url('+tbCustomIcons[i-1]+')');
			else
				$('#'+tBVJSId+'innerDivId'+i).addClass('MissingIconClass');
		}
		else
		{
		 	if(standeredIcon)
			{
				
				if(tbStandardIcons[i-1])
					$('#'+tBVJSId+'innerDivId'+i).addClass(tbStandardIcons[i-1]+"ImageClass");
				else
					$('#'+tBVJSId+'innerDivId'+i).addClass("MissingIconClass");
			}
		}
		$('#'+tBVJSId+'innerDivId'+i).click (bgChangeClickFn);
		if(tbHideLabel)
			$('#'+tBVJSId+'innerDivId'+i).css('background-position','center');
	}
	// correct the offsets appearing for the widget Div
	this.getWidgetDiv().css('lineHeight', 0);
	if($.browser.msie)// for IE
		this.getWidgetDiv().css('marginTop', 0);
	else if($.browser.opera) // for Opera
		this.getWidgetDiv().css('marginLeft', 0.5);
	else if($.browser.safari){// for Safari
		if((parseInt($.browser.version))>=534)
			this.getWidgetDiv().css('marginTop', 0);
		else
			this.getWidgetDiv().css('marginTop', -5);
	}
	if (navigator.userAgent.toLowerCase().indexOf('chrome') > -1) //Chrome
			this.getWidgetDiv().css('marginTop', 0);
		
function bgChangeClickFn()
{
	var cid = $(this).attr('id');
	cid = cid.substr(4);
	for(var i = 1;i<= tbarLabeles.length;i++)
	{
		if(cid =="innerDivId"+i)//for selected tab
		{
			$('#'+forDivId+' .TBVJSinnerclass'+i).css('color', selectedLabelColor);
			if(default_hlit)
			{
					$('#'+tBVJSId+'innerContentDivId'+i).addClass('HighlightMiddleClass');
					$('#'+tBVJSId+'innerContentDivIdLeft'+i).addClass('HighlightLeftClass');
					$('#'+tBVJSId+'innerContentDivIdRight'+i).addClass('HighlightRightClass');
			}
			
			if(customIcon)//custom iconsss
			{
				if(tbCustomSelectedIcons[i-1] == "" || tbCustomSelectedIcons[i-1] == null || tbCustomSelectedIcons[i-1] == "undefined")
				{
					$('#'+tBVJSId+'innerDivId'+i).css('background-image', '');
					$('#'+tBVJSId+'innerDivId'+i).addClass('MissingIconSelectedClass');
				}
				else
					$('#'+tBVJSId+'innerDivId'+i).css('background-image', 'url('+tbCustomSelectedIcons[i-1]+')');
				
			}
			else//standered icons
			{
				if(!bgImg)
				{
					$('#'+tBVJSId+'innerContentDivId'+i).addClass('HighlightTabClass'+tbarLabeles.length);
					//$('#'+tBVJSId+'innerContentDivId'+i).addClass('HighlightMiddleClass');
					$('#'+tBVJSId+'innerContentDivIdLeft'+i).addClass('HighlightLeftClass');
					$('#'+tBVJSId+'innerContentDivIdRight'+i).addClass('HighlightRightClass');
					$('#'+tBVJSId+'innerDivId'+i).removeClass(tbStandardIcons[i-1]+"ImageClass");
				}
				if(tbStandardIcons[i-1])
					$('#'+tBVJSId+'innerDivId'+i).addClass(tbStandardIcons[i-1]+"ImageSelectedClass");
				else
					$('#'+tBVJSId+'innerDivId'+i).addClass('MissingIconSelectedClass');
			}
			if(prevDivSelected != cid)
			{
				me.sendEvent('TB_OnLabelClick'+i);
				setTabBarVerticalJSProperties(tbarLabeles[i-1],me);
				
			}
			prevDivSelected = cid;
		}
		else//unselected tabs
		{
			//$('#TBVJSinnerclass'+i).css('background-image', 'url('../images/OuterDivBGImage.png')');
			$('#'+forDivId+' .TBVJSinnerclass'+i).css('color', TBVJSlabelColor);
			$('#'+tBVJSId+'innerContentDivId'+i).removeClass('HighlightTabClass'+tbarLabeles.length);
			//$('#'+tBVJSId+'innerContentDivId'+i).removeClass('HighlightMiddleClass');
			$('#'+tBVJSId+'innerContentDivIdLeft'+i).removeClass('HighlightLeftClass');
			$('#'+tBVJSId+'innerContentDivIdRight'+i).removeClass('HighlightRightClass');
			$('#'+tBVJSId+'innerDivId'+i).removeClass('MissingIconSelectedClass');
			if(customIcon)
			{
				//alert(tbCustomIcons[i-1]);
				if(tbCustomIcons[i-1])
					$('#'+tBVJSId+'innerDivId'+i).css('background-image', 'url('+tbCustomIcons[i-1]+')');
				else
				{
					//$('#'+tBVJSId+'innerDivId'+i).css('background-image', 'none');
					$('#'+tBVJSId+'innerDivId'+i).css('background-image', '');
					$('#'+tBVJSId+'innerDivId'+i).addClass('MissingIconClass');
				}
			}
			else
				{
					$('#'+tBVJSId+'innerDivId'+i).removeClass(tbStandardIcons[i-1]+"ImageSelectedClass");
					$('#'+tBVJSId+'innerDivId'+i).addClass(tbStandardIcons[i-1]+"ImageClass");
					
				}
			
		}
	}
}
	var selectedIndex = jQuery.inArray(tbSelectedTab,tbarLabeles)+1;//onLoad heighlight conditions
	$('#'+forDivId+' .TBVJSinnerclass'+selectedIndex).css('color', selectedLabelColor);
	if(default_hlit )
	{
		$('#'+tBVJSId+'innerContentDivId'+selectedIndex).addClass('HighlightTabClass'+tbarLabeles.length);
		$('#'+tBVJSId+'innerContentDivId'+selectedIndex).addClass('HighlightMiddleClass');
		$('#'+tBVJSId+'innerContentDivIdLeft'+selectedIndex).addClass('HighlightLeftClass');
		$('#'+tBVJSId+'innerContentDivIdRight'+selectedIndex).addClass('HighlightRightClass');
	}
	if(customIcon)
	{
		if(tbCustomSelectedIcons[selectedIndex-1] == "" || tbCustomSelectedIcons[selectedIndex-1] == null || tbCustomSelectedIcons[selectedIndex-1] == "undefined")
		{
			$('#'+tBVJSId+'innerDivId'+selectedIndex).css('background-image', '');
			$('#'+tBVJSId+'innerDivId'+selectedIndex).addClass('MissingIconSelectedClass');
		}
		else
			$('#'+tBVJSId+'innerDivId'+selectedIndex).css('background-image', 'url('+tbCustomSelectedIcons[selectedIndex-1]+')');
	}
	else
	{
		if(!bgImg)
		{
			$('#'+tBVJSId+'innerContentDivId'+selectedIndex).addClass('HighlightTabClass'+tbarLabeles.length);
			$('#'+tBVJSId+'innerContentDivId'+selectedIndex).addClass('HighlightMiddleClass');
			$('#'+tBVJSId+'innerContentDivIdLeft'+selectedIndex).addClass('HighlightLeftClass');
			$('#'+tBVJSId+'innerContentDivIdRight'+selectedIndex).addClass('HighlightRightClass');
		}
		$('#'+tBVJSId+'innerDivId'+selectedIndex).removeClass(tbStandardIcons[selectedIndex-1]+"ImageClass");
		if(tbStandardIcons[selectedIndex-1])
			$('#'+tBVJSId+'innerDivId'+selectedIndex).addClass(tbStandardIcons[selectedIndex-1]+"ImageSelectedClass");
		else
			$('#'+tBVJSId+'innerDivId'+selectedIndex).addClass("MissingIconSelectedClass");
	}
	me.setProperty('tbSelectedTab',tbSelectedTab);	// sets the widget property
	if(me.getProperty('tbTargtWidget')){
		if (me.getProperty('tbTargtWidget').getType() === 'widget') // if an ibloc, then use the setValue acti
				me.getProperty('tbTargtWidget').setValue(tbSelectedTab);
		else{ 
			var targetWidgetId = me.getProperty('tbTargtWidget').id;
			if(me.getProperty('tbTargtWidget').getWidgetDiv().text()&& !$("#"+targetWidgetId).val()){ // if label widget, then use html() method
				me.getProperty('tbTargtWidget').getWidgetDiv().html(tbSelectedTab);
				// iPhoneValuePickerVerticalInstanceArr[instanceId].getProperty('dpTargtWidget').getWidgetDiv().show();
			}
			else// for all others, use the val() method
				$("#"+targetWidgetId).val(tbSelectedTab); 
		}
	}
//onload end	
}

function setTabBarVerticalJSProperties(val,thisWidget)
{
	thisWidget.setProperty('tbSelectedTab',val);	// sets the widget property
	if(thisWidget.getProperty('tbTargtWidget')){
		if (thisWidget.getProperty('tbTargtWidget').getType() === 'widget') // if an ibloc, then use the setValue acti
			thisWidget.getProperty('tbTargtWidget').setValue(val);
		else{ 
			var targetWidgetId = thisWidget.getProperty('tbTargtWidget').id;
			if(thisWidget.getProperty('tbTargtWidget').getWidgetDiv().text() && !$("#"+targetWidgetId).val()){ // if label widget, then use html() method
				thisWidget.getProperty('tbTargtWidget').getWidgetDiv().html(val);
				// intId[instanceId].getProperty('dpTargtWidget').getWidgetDiv().show();
			}
			else
				$("#"+targetWidgetId).val(val); 
		}
		//Highlight the selected value
	}
/*if(fireEvent)
		iPhoneTBVJSInstanceArr[intId].sendEvent('OnSelect');*/
	    return "success";
}

irise.iPhoneTabBarVertical.prototype.setValue = function(c){
	var tbarLabeles = this.getProperty('tbLabels');
	var previousTBVJSValue = this.getProperty('tbSelectedTab');
	if ($.isArray(c))
		var setValTBVJS = c[0];
	else
		var setValTBVJS = c;

	 var inputTabValue = jQuery.inArray(setValTBVJS, tbarLabeles);
	if(previousTBVJSValue != setValTBVJS && inputTabValue >=0)
		{
			inputTabValue++;
			//prevDivSelected = "innerDivId"+inputTabValue;
			this.sendEvent('TB_OnLabelClick'+inputTabValue);
			this.setProperty('tbSelectedTab', setValTBVJS);
			$(this.getWidgetDiv()).children().remove();
			this.onload();
			//alert(inputTabValue);
			
		}// sets the widget property			
 }